Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ reference to a vector issues?

Im having some difficulties when passing a reference to a vector as an argument. Initially it was a pointer to a vector of pointers, and the functions called would change attributes of whatever obejct was calling it. For example I used:

(*myVector)[i]->myFunction();

and everything worked perfectly. myFunction() would translate whichever object was the current index of myVector.

Now i've changed the vector to hold a set of objects instead of pointers to the objects as it did originally. Instead of passing a pointer to the vector, i pass a reference of it. I access the function as follows:

myVector[i].myFunction();

It compiles, I find that myFunction() no longer has the same results as it did before. I used a breakpoint and saw that it was changing attributes it should have been, however, those changes never made it to the original object. There was no final translation even though the breakpoint showed the origin of myVector[i] being changed.

I've tried passing a pointer to the vector of objects and used:

(*myVector)[i].myFunction();

and the same thing, it compiled but didn't work.

Finally i tried simply passing a reference to the first object in the vector, and of course that worked just fine:

myObject.myFunction();

Is there some inherent thing im missing about references of vectors? Why does a pointer to a vector of pointers work but a reference to a vector of objects doesnt? Shouldn't the passed vector be an alias to the original vector? Why is the reference showing changes but not the object it referenced?

Why wont my changes stick unless i used a vector of pointers instead of a vector of objects.

like image 247
kbirk Avatar asked Oct 27 '25 10:10

kbirk


1 Answers

Hold on a minute. When you changed this snippet:

(*myVector)[i]->myFunction()

To this:

myVector[i].myFunction()

You've actually changed two things:

  1. myVector went from pointer to reference;
  2. The type of the elements inside myVector went from pointer to simple value type.

If you changed the type of the elements to a simple value type (as in std::vector<int*> to std::vector<int>), then when you add them to your vector, they are copied to it. (The way the vector is passed around, that is, by pointer or by reference, doesn't affect this behavior. The way the vector is passed and the way the objects inside the vector are passed are completely independent.) This mean that whenever you make a change to them, you only change the version that's inside the vector.

There's nothing wrong with using a vector that holds pointers (as long as you do your memory management in a sensible way). If you want the changes to the version you have in your vector to be replicated to the originals, you'll have to use references or pointers. Since it's a world of pain to have a vector contain references (as in std::vector<int&>), if at all possible, you would probably prefer pointers.

like image 176
zneak Avatar answered Oct 30 '25 00:10

zneak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!