CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Object Destruction Question

    Hey, my question is pretty simple:
    Code:
    #include <memory>
    #include <iostream>
    using namespace std;
    
    class A
    {
    	public:
    	A(){
    		cout << "A's constructor" << endl;
    	}
    
    	~A(){
    		cout << "A's destructor" << endl;
    	}
    	
    	void testMe(){
    		cout << "testMe from A" << endl;
    	}
    };
    
    auto_ptr<A> test(){
    	return auto_ptr<A>(new A());
    }
    
    int main(){
    	test()->testMe();
    }
    Now, this generated the desierable output:
    A's constructor
    testMe from A
    A's destructor
    Okay, so when I stepped through this code I noticed that that output that I posted above appeared all in one shot.

    So, it seems that when I call test, a new A() object is generated, then I call testMe, so far that's what I thought is supposed to happen, but next thing, just before the return, the destructor is called!

    I also noticed this behaviour when chaning function calls like this with cout and whatnot, is this the way it works?

    I mean, is the object always destructed after the function call right away regardless of scope issues?

    So for example, if I had
    Code:
    int testMe()
    {
    return 12345;
    }
    
    void someOtherFunc()
    {
    testMe();
    
    someothercall();
    }
    the result (12345) from the functino call to testMe() would be destroyed before someothercall() was ever reached?

    I thought this all depended on scope, guess I was wrong.

    Does anyone have any comments on this? Will this always work?

    Thanks in advance!

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757

    Re: Object Destruction Question

    not familiar with smart pointers. assign variable to value returned from function

    auto_ptr<a> b()
    {
    return auto_ptr<a>(new a)
    }

    auto_ptr<a> c = b()

    Kuphryn

  3. #3
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Object Destruction Question

    Agree with Kuphryn's analysis.

    The test() function returns a temporary object, which (per the C++ standard) is destroyed immediately after execution of the statement is completed. See http://msdn2.microsoft.com/en-us/library/a8kfxa78.aspx

    It should always work, so long as the compiler is conforming. But in some instances/compilers it does not. For one example, see this post on the microsoft.public.vc.mfc newsgroup: "I find a BIG bug of VS 2005 about string class!" at http://groups.google.com/group/micro...09dfc8084b32e9 . The posted example should not have worked at all, and in fact it didn't work on VS 2005, but it worked in Dev C++.

    Mike
    Last edited by MikeAThon; September 5th, 2006 at 03:36 PM.

  4. #4
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Object Destruction Question

    Quote Originally Posted by MikeAThon
    Agree with Kuphryn's analysis.
    The test() function returns a temporary object, which (per the C++ standard) is destroyed immediately after execution of the statement is completed. See http://msdn2.microsoft.com/en-us/library/a8kfxa78.aspx
    Thanks guys, that answered all my questions!

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Object Destruction Question

    operator->() in auto_ptr is a const-function so it is legal.

    As for the other behaviour, auto_ptr was designed in such a way that you can return it from a function and the pointer is transferred. In C++0x I think it will be deprecated in place of unique_ptr which carries a bool as well as the pointer, and only the ownership, not the pointer, is transferred, and then only on an implicit call to move(), not on a copy-constructor. There will be a different notation for return values that are moved.

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