I have a class that should, as its input data, either use a reference to external data (without copying), or create the data itself based on other input. I prefer using references (to avoid dereferencing, since the data are matrices) and ended up with the following structure (simplified):
#include <iostream> #include <vector> using namespace std; using VectI = vector<int>; class A { VectI x0; VectI const & x = x0; public: A(VectI const & extX) : x(extX) {} // referencing existing external data A(int i) { x0.push_back(i); x0.push_back(i*i); } // create from other data void show_x() { cout << "x ="; for (auto&& i : x) cout << " " << i; cout << endl; } }; int main() { VectI myX = {1, 2, 3}; A a(myX); a.show_x(); // a references myX A b(2); b.show_x(); // b creates its own data based on i=2 return 0; } The example works:
x = 1 2 3 x = 2 4 but are there any potential problems with this approach? In particular, is the fact that I am changing x0 that is referenced by the const vector x 'legal' C++, or is it something that other compilers might complain about?
Also, can I be sure that the first constructor avoids copying data?
xis a reference.A a(VectI(3)); a.show_x();.