Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Thingy{
- private:
- std::vector<int> stuff; // stuff
- public:
- Thingy(){
- stuff = {1,2,3};
- }
- std::vector<int> getStuff(){
- return stuff;
- }
- std::vector<int>& getStuffRef(){
- return stuff;
- }
- void printstuff(){
- for (int i = 0; i < stuff.size(); ++i){
- printf("%d, ", stuff[i]);
- }
- printf("\n");
- }
- };
- // actual code
- Thingy thing = Thingy();
- std::vector<int> theStuff = thing.getStuff();
- theStuff.append(4);
- thing.printstuff();
- /* this will print
- 1, 2, 3,
- because we got a copy of the vector, not a reference, so the original wasn't changed by our append */
- theStuff = thing.getStuffRef();
- theStuff.append(4);
- thing.printstuff();
- /* this will print
- 1, 2, 3, 4,
- because we got a reference to the vector in the object, so the original WAS changed by our append */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement