By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

Is there a temporal object created?

P: n/a
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.

--
Hongzheng Wang

Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Hongzheng Wang" <wa******@mails.tsinghua.edu.cn> wrote in message
news:br**********@news.yaako.com...
| 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
Jul 22 '05 #2

P: n/a
On Mon, 08 Dec 2003 19:55:51 +0800, Hongzheng Wang
<wa******@mails.tsinghua.edu.cn> wrote:
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
Jul 22 '05 #3

P: n/a
Thank you all!
Thank you very much :-)

--
Hongzheng Wang

Jul 22 '05 #4

P: n/a
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.

--
Hongzheng Wang

Jul 22 '05 #5

P: n/a
Hongzheng Wang wrote in news:br**********@news.yaako.com:
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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.