Consider the following example of a simple class implementation in C++.
foo.hpp
#include <vector>
class Foo {
private:
std::vector<double> X;
public:
Foo() = default;
~Foo() = default;
Foo(std::vector<double>&);
};
foo.cpp
#include "Foo.hpp"
Foo::Foo(std::vector<double>& X):
X(X)
{}
In this case, the vector X is passed by reference to the constructor of the class Foo. I start having doubts, though, on whether the operation X(X) in the implementation makes a copy and pastes it "in" the member X of the class.
Which is it?
>Solution :
Yes, the data member X will be copy-initialized from the constructor parameter X.
If you declare the data member X as reference, then no copy operation happens. E.g.
class Foo {
private:
std::vector<double>& X;
public:
~Foo() = default;
Foo(std::vector<double>&);
};
Foo::Foo(std::vector<double>& X):
X(X)
{}
Then
std::vector<double> v;
Foo f(v); // no copies; f.X refers to v