I’m a C++ noob programmer… (I usually use Java) and now I’ve got some problems with pointers, references and vectors…
I need to extract an Object from vector<Object> objects and for simplicity i would like to store his reference in another object cause I need to edit some parameters inside of it many times
this is the code i’ve written so far:
Object myObject = getMyObject(id, objects);
and getMyObject simply implements a for cycle which finds the right object with the right id and it returns it with return objects[i]
but if I understood right, this method duplicates the objects! so, in myObject there’s a completely different Object, is it right?
How should I do??
Thank you very much!
You’re right. What you should do is have the get function return a reference:
Then bind this to another reference in your user code:
Now you modify the contained element directly via the reference
theobject.If you don’t want/need to modify the object, just inspect them, you should make the reference (as well as the container)
const: