should I make this a member variable?
Code:
virtual void initModels()
{
mWarehouseMesh = new SkinnedMesh();
mWarehouseMesh->Load(L"Data/DemoWarehouse.x");
Objects warehouseObj(mWarehouseMesh);
warehouseObj.setPos(D3DXVECTOR3(0,0,0));
objs.push_back(&warehouseObj);
mCBMesh = new SkinnedMesh();
mCBMesh->Load(L"Data/cb.x");
Objects cbObject(mCBMesh);
cbObject.setPos(D3DXVECTOR3(10,10,10));
objs.push_back(&cbObject);
}
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?
Code:
{
Objects warehouseObj(mWarehouseMesh);
//...
objs.push_back(&warehouseObj);
Objects cbObject(mCBMesh);
//...
objs.push_back(&cbObject);
}
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.
Quote:
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.
Quote:
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.
Regards,
Paul McKenzie
Re: should I make this a member variable?
Does that mean I only have 2 options?
1) global variable
2) member variable of the class
No other ways will make this "Object remain persistent throughout the lifetime of this program?
Re: should I make this a member variable?
Quote:
Originally Posted by
lucky6969b
Does that mean I only have 2 options?
1) global variable
2) member variable of the class
No other ways will make this "Object remain persistent throughout the lifetime of this program?
I don't understand why option 2) is listed:
Code:
void foo()
{
std::vector<int> v;
}
The v is an instance of the vector<int> class, it has members, and v is dead when foo() returns.
Regards,
Paul McKenzie
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.
Regards,
Paul McKenzie
Re: should I make this a member variable?
Thanks Paul,
With something like this objs.push_back(new Objects(mOperatorMesh)); does that trick.
Thanks
Jack