I have a class like this:
class Foo { public: Foo() { for(int i = 0; i < 10; ++i) v.push_back(i); }; const vector<double>& V() const {return v;}; protected: vector<double>& V() {return v;}; private: vector<double> v; };
And then the code snippet is as follows:
Foo foo; for(int i = 0; i < (int) foo.V().size(); ++i) cout << foo.V().at(i) << endl; () size (); ++ i) Foo foo; for(int i = 0; i < (int) foo.V().size(); ++i) cout << foo.V().at(i) << endl;
However, the latter is a compilation error, saying that the call to V() is a secure method as long as I'm just trying to read it, not change it.
I tried the following (but unsuccessfully).
Foo foo; const vector<double>& test = foo.V(); for(int i = 0; i < (int) test.size(); ++i) cout << test.at(i) << endl; foo.V (); Foo foo; const vector<double>& test = foo.V(); for(int i = 0; i < (int) test.size(); ++i) cout << test.at(i) << endl; ); ++ i) Foo foo; const vector<double>& test = foo.V(); for(int i = 0; i < (int) test.size(); ++i) cout << test.at(i) << endl;
Many thanks for your help.
=====
Thank you all for explanations and solutions! He is very grateful!
source share