Is there a temporal object created?

H

Hongzheng Wang

Hi,

If I have such codes:

class fred {
//user defined constructors/destructor here
};

int main()
{
fred* p = &fred(); //(1)
//do something else...
return 0;
}

My compiler complained having assigned address of a temporal
object on line (1).
And I noticed that the fred's destructor is called immediatelly
after its constructor is called on line (1).
I'm wonder why a temporal object is created here.
Can anyone give some explanation?

Thank you very much.
 
C

Chris \( Val \)

| Hi,
|
| If I have such codes:
|
| class fred {
| //user defined constructors/destructor here
| };
|
| int main()
| {
| fred* p = &fred(); //(1)
| //do something else...
| return 0;
| }
|
| My compiler complained having assigned address of a temporal
| object on line (1).
| And I noticed that the fred's destructor is called immediatelly
| after its constructor is called on line (1).
| I'm wonder why a temporal object is created here.
| Can anyone give some explanation?

Because that is exactly what you have told it to do :).

The compiler needs to create an temporary object, so it's
address can then be stored in 'p'.

'fred()' is an un-named(temporary) object, of which you
are asking for an address.

Cheers.
Chris Val
 
T

tom_usenet

Hi,

If I have such codes:

class fred {
//user defined constructors/destructor here
};

int main()
{
fred* p = &fred(); //(1)
//do something else...
return 0;
}

My compiler complained having assigned address of a temporal
object on line (1).

Yes, the code is illegal - you can't take the address of a temporary
object.
And I noticed that the fred's destructor is called immediatelly
after its constructor is called on line (1).

If the code were to compile (a compiler extension of some kind), that
is what I'd expect - temporaries only last until the end of the full
expression in which they are created. In this case, that full
expression is "fred* p = &fred();".
I'm wonder why a temporal object is created here.
Can anyone give some explanation?

You created a temporary - fred() creates an unnamed temporary fred
object. I think you wanted either:

fred f; //destroyed at end of scope.
fred* p = &f;

or possibly:
fred* p = new fred; //dynamic creation, requires delete
//...
delete p;

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
H

Hongzheng Wang

I have still a problem about this topic.

fred* p = &fred();

A temporal unnamed object will be created here,
and its lifetime is the expression.

Then,
fred obj = fred();
is there a temporal (unnamed?) object will be created
also?

Does the invocation `fred()' will always create temporal
object?

Thank you.
 
R

Rob Williscroft

Hongzheng Wang wrote in
I have still a problem about this topic.

fred* p = &fred();

A temporal unnamed object will be created here,
and its lifetime is the expression.

If the code were not illegal, then yes.
Then,
fred obj = fred();
is there a temporal (unnamed?) object will be created
also?

Yes and this temporary, is copied to obj using the copy constructor.

There are a whole bunch of rules that allow the compiler to effectivly
optimise away the copy, so the above can be as effecient as:

fred obj; /* default constructor called */

BTW don't try:

free obj(); as its a declaration of function returning a fred.
Does the invocation `fred()' will always create temporal
object?

Yes.

Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,151
Messages
2,570,854
Members
47,394
Latest member
Olekdev

Latest Threads

Top