CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2001
    Location
    Oslo, Norway
    Posts
    610

    What is the lifetime of unnamed object of a class ?

    Helo all

    Say i have a class

    Code:
    class A
    {
    
    };
    and two functions f and g

    Code:
    void f()
    {
          A();
    
          return calculateSomething();
    }
    
    void g()
    {
         A obj;
    
         return calculateSomething();
    }
    The differences between f and g appear to be the fact that unnamed object of class A in f is destroyed before calculateSomething is even called - you can say it lives only in that single line ( A(); )

    The object named 'obj' rather lives until the function returns, which is of course understood.

    THis is compiled under VC++ 7.0.

    Is it right about the lifetime of unnamed object of class A ? Shouldnt their lifetimes be the same ?

  2. #2
    Join Date
    Oct 2002
    Posts
    36
    No, because you are telling the compiler to create 2 different types of object

    When you call A(), you are creating a temporary object that will exist only for the life of the of line of code on which it was created. As soon as that section of code ends, the object's destructor is called. These temps are often used for example when you return an object by value...ect

    When you define A obj, you are defining an object that will have scope inside that block. When the code block ends, the destuctor is called

    For example

    Code:
    #include <iostream>
    
    class A
    {
    public:
    	A(){std::cout << "Con" << std::endl;};
    	~A(){std::cout << "Des" << std::endl;};
    };
    
    void f(){
    	A();
    	std::cout << "End of f()" << std::endl;
    } 
    
    void g(){
    	A a;
    	std::cout << "End of g()" << std::endl;
    } 
    
    int main(void){	
    	
    	
    	f();
    	std::cout << std::endl;
    	g();
    	
    }

  3. #3
    Join Date
    May 2001
    Location
    Oslo, Norway
    Posts
    610
    Thanks a lot !

    Everything works as it should now, and all the clouds in my thoughts disappeared !

  4. #4
    Join Date
    Oct 2002
    Location
    Florida
    Posts
    33
    Examine the function f(). Consider the statement

    A();

    This does not represent an object of class A. This statement is simply an invocation of a function called "A" that takes no arguments. Because of the syntax clarity, there is no namespace conflict between a function called A and a class named A.

    In other words, the statement A() has nothing to do with the class named A. This statement may compile, but it should not link unless there is a function "A" with appropriate linkage with respect to f().

  5. #5
    Join Date
    May 2001
    Location
    Oslo, Norway
    Posts
    610
    It maybe invocation, but it compiles and steps into A's constructor when stepped from in Debugger.

  6. #6
    Join Date
    Oct 2002
    Location
    Florida
    Posts
    33
    My apologies to all. The explanation of dumah is absolutely correct. In the future, I'll test my answers before publishing them :|

    Of interest (perhaps): If there was indeed a global function A(), it would have been called rather than the temporary A object created. This ambiguity is referenced in Stroustrup's ARM 9.1.

    I had previously thought that the only interpretation of A() was a function invocation.

  7. #7
    Join Date
    Jul 2002
    Location
    American Continent
    Posts
    340
    Examine the function f(). Consider the statement

    A();

    This does not represent an object of class A. This statement is simply an invocation of a function called "A" that takes no arguments. Because of the syntax clarity, there is no namespace conflict between a function called A and a class named A.

    In other words, the statement A() has nothing to do with the class named A. This statement may compile, but it should not link unless there is a function "A" with appropriate linkage with respect to f().
    So that others may not be mislead, I must point out that the above statement is completely wrong.

  8. #8
    Join Date
    Oct 2002
    Posts
    36
    Originally posted by AnthonyMai
    So that others may not be mislead, I must point out that the above statement is completely wrong.
    He already pointed this out himself, does it need you to reiterate this?

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