I have a function A(), that returns a pointer to an object. In function B() I try to change a member of that object in the following way:
void B() { ObjType o = *getObj(); o.set('abc'); }
Object o is stored in an array, and when I print the value of the member, it seems nothing happened, and the member still has the old value;
The solution is quite simple:
void B() { ObjType * o = getObj(); o->set('abc'); }
This does work. But to me, this is quite the same as the first sample. Can anyone explain this?
The following line is most likely copying the object:
That’s why nothing happens. If you don’t want to use a pointer as shown in your second snippet, you can use a reference like this: