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....
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:
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.
Last edited by Paul McKenzie; February 15th, 2013 at 02:12 AM.
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.