CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2010
    Posts
    907

    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

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    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.
    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.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 15th, 2013 at 03:12 AM.

  3. #3
    Join Date
    Dec 2010
    Posts
    907

    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?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: should I make this a member variable?

    Quote Originally Posted by lucky6969b View Post
    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

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    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

  6. #6
    Join Date
    Dec 2010
    Posts
    907

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured