Click to See Complete Forum and Search --> : What is the lifetime of unnamed object of a class ?


Amn
October 27th, 2002, 12:44 PM
Helo all :)

Say i have a class



class A
{

};



and two functions f and g



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 ?

dumah
October 27th, 2002, 01:06 PM
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


#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();

}

Amn
October 27th, 2002, 02:11 PM
Thanks a lot ! :D

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

BaroloMan
October 28th, 2002, 07:52 AM
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().

Amn
October 28th, 2002, 08:18 AM
It maybe invocation, but it compiles and steps into A's constructor when stepped from in Debugger.

BaroloMan
October 28th, 2002, 08:51 AM
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.

AnthonyMai
October 28th, 2002, 09:17 AM
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.

dumah
October 28th, 2002, 09:41 AM
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?