If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Note that cbObject + warehouseObj are transient, if I invoke its methods in the objs array, everything will be trashed inside it. Should I make this a member variable? The thing is that it is not a neat idea to do so....
Any ideas?
Thanks
Jack

Re: should I make this a member variable?

What did you expect to happen with this? You're pushing onto the vector the address of a local variable. So what happens when that function returns? That local variable is now gone.

if I invoke its methods in the objs array, everything will be trashed inside it

Of course it will be trashed. The reason why is explained above.

The question is why did you even consider writing this code, unless you really thought it would work. It doesn't matter what contortions you try to make your local variables hold non-local data, the end results will be the same -- storing the address of a local variable in a vector (or any container) and then expecting that local variable to magically still exist after the function has returned is totally wrong.

Doing this beforehand:

Code:

mWarehouseMesh = new SkinnedMesh();

And then storing this inside that local object doesn't save you.

Should I make this a member variable? The thing is that it is not a neat idea to do so....

The issue you have is object lifetime -- simply put, you can't use address of local variables and expect those addresses to point to valid data after exit of a functional block.

Re: should I make this a member variable?

Any variable, whether it is a simple variable, or an instance of a struct/class, an array, whatever, has the same rules imposed on it when it comes to lifetime. That's why your option 2) doesn't apply.

Any global variable, whether it is an int, double, float, or Widget, will have lifetime throughout the running of the program. A dynamically allocated object, unless it is deleted, has lifetime from the time it is dynamically allocated.

A member variable, as you saw in my post above, relies on the object instance to be alive. If the object dies, the members of the object die or can no longer be accessed.