 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Joe Van Dyk Guest
|
Posted: Mon May 15, 2006 11:21 pm Post subject: Silly returning-an-object question |
|
|
Hi,
struct Foo {};
Foo oogie_boogie()
{
Foo a;
return a;
}
oogie_boogie() returns a reference to a Foo object, right? (so, when I
call oogie_boogie(), only one Foo object is created?)
Thanks,
Joe |
|
| Back to top |
|
 |
Roy Smith Guest
|
Posted: Mon May 15, 2006 11:21 pm Post subject: Re: Silly returning-an-object question |
|
|
Joe Van Dyk <joe.vandyk (AT) boeing (DOT) com> wrote:
| Quote: | Hi,
struct Foo {};
Foo oogie_boogie()
{
Foo a;
return a;
}
oogie_boogie() returns a reference to a Foo object, right?
|
No, it returns a Foo. If you wanted it to return a reference, you
would have had to declare the function:
Foo& oggie_boogie();
| Quote: | (so, when I call oogie_boogie(), only one Foo object is created?)
|
In theory, the "Foo a;" line will construct a Foo, and then the
"return a;" will copy-construct another Foo. In practice, almost any
compiler will optimize away the extra copy-construct and just create
the one object. |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Mon May 15, 2006 11:21 pm Post subject: Re: Silly returning-an-object question |
|
|
Joe Van Dyk wrote:
| Quote: | Hi,
struct Foo {};
Foo oogie_boogie()
{
Foo a;
return a;
}
oogie_boogie() returns a reference to a Foo object, right? (so, when I
call oogie_boogie(), only one Foo object is created?)
It returns an instance of a Foo, not a reference. |
The compiler will probably optimise away the extra copy.
--
Ian Collins. |
|
| Back to top |
|
 |
Guest
|
Posted: Tue May 16, 2006 2:21 am Post subject: Re: Silly returning-an-object question |
|
|
Roy Smith wrote:
| Quote: | Joe Van Dyk <joe.vandyk (AT) boeing (DOT) com> wrote:
Hi,
struct Foo {};
Foo oogie_boogie()
{
Foo a;
return a;
}
oogie_boogie() returns a reference to a Foo object, right?
No, it returns a Foo. If you wanted it to return a reference, you
would have had to declare the function:
Foo& oggie_boogie();
|
But of course we would never want to return a reference to a temporary,
would we?  |
|
| Back to top |
|
 |
Roy Smith Guest
|
Posted: Tue May 16, 2006 2:21 am Post subject: Re: Silly returning-an-object question |
|
|
In article <1147744148.342209.257640 (AT) v46g2000cwv (DOT) googlegroups.com>,
indrawati.yahya (AT) gmail (DOT) com wrote:
| Quote: | Roy Smith wrote:
Joe Van Dyk <joe.vandyk (AT) boeing (DOT) com> wrote:
Hi,
struct Foo {};
Foo oogie_boogie()
{
Foo a;
return a;
}
oogie_boogie() returns a reference to a Foo object, right?
No, it returns a Foo. If you wanted it to return a reference, you
would have had to declare the function:
Foo& oggie_boogie();
But of course we would never want to return a reference to a temporary,
would we?
|
It depends. We might want to be testing our memory corruption detection
system  |
|
| Back to top |
|
 |
Joe Van Dyk Guest
|
Posted: Tue May 16, 2006 2:21 am Post subject: Re: Silly returning-an-object question |
|
|
Ian Collins wrote:
| Quote: | Joe Van Dyk wrote:
Hi,
struct Foo {};
Foo oogie_boogie()
{
Foo a;
return a;
}
oogie_boogie() returns a reference to a Foo object, right? (so, when I
call oogie_boogie(), only one Foo object is created?)
It returns an instance of a Foo, not a reference.
The compiler will probably optimise away the extra copy.
|
Ah, ok. That optimization is probably why I was thinking that a
reference was being returned.
If oogie_boogie() returned a reference to a Foo object, then would
Foo &f(oogie_boogie());
be ok?
Joe |
|
| Back to top |
|
 |
Roy Smith Guest
|
Posted: Tue May 16, 2006 2:21 am Post subject: Re: Silly returning-an-object question |
|
|
In article <IzC4HI.E1v (AT) news (DOT) boeing.com>,
Joe Van Dyk <joe.vandyk (AT) boeing (DOT) com> wrote:
| Quote: | Ian Collins wrote:
Joe Van Dyk wrote:
Hi,
struct Foo {};
Foo oogie_boogie()
{
Foo a;
return a;
}
oogie_boogie() returns a reference to a Foo object, right? (so, when I
call oogie_boogie(), only one Foo object is created?)
It returns an instance of a Foo, not a reference.
The compiler will probably optimise away the extra copy.
Ah, ok. That optimization is probably why I was thinking that a
reference was being returned.
If oogie_boogie() returned a reference to a Foo object, then would
Foo &f(oogie_boogie());
be ok?
Joe
|
OK for what? I'm not sure what you're trying to do here. |
|
| Back to top |
|
 |
Old Wolf Guest
|
Posted: Tue May 16, 2006 3:22 am Post subject: Re: Silly returning-an-object question |
|
|
Joe Van Dyk wrote:
| Quote: | Ian Collins wrote:
Joe Van Dyk wrote:
struct Foo {};
Foo oogie_boogie()
{
Foo a;
return a;
}
If oogie_boogie() returned a reference to a Foo object, then would
Foo &f(oogie_boogie());
be ok?
|
It would compile correctly. But in order to run correctly you
would have to ensure that the returned object still existed
when you try and use 'f' (which is not the case in the above
version of oogie_boogie). |
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Sun May 21, 2006 2:21 pm Post subject: Re: Silly returning-an-object question |
|
|
indrawati.yahya (AT) gmail (DOT) com wrote:
| Quote: | But of course we would never want to return a reference to a temporary,
would we? ;)
|
It's not a temporary, just a reference to an object whose lifetime
has ended. |
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Sun May 21, 2006 4:21 pm Post subject: Re: Silly returning-an-object question |
|
|
Sjouke Burry wrote:
| Quote: | Ron Natalie wrote:
indrawati.yahya (AT) gmail (DOT) com wrote:
But of course we would never want to return a reference to a
temporary, would we? ;)
It's not a temporary, just a reference to an object whose lifetime
has ended.
You have just now defined temporary -)-)
|
There has to be some difference between temporary and expired... |
|
| Back to top |
|
 |
Sjouke Burry Guest
|
Posted: Sun May 21, 2006 4:21 pm Post subject: Re: Silly returning-an-object question |
|
|
Ron Natalie wrote:
| Quote: | indrawati.yahya (AT) gmail (DOT) com wrote:
But of course we would never want to return a reference to a temporary,
would we? ;)
It's not a temporary, just a reference to an object whose lifetime
has ended.
You have just now defined temporary -)-) |
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Mon May 22, 2006 11:21 am Post subject: Re: Silly returning-an-object question |
|
|
Sjouke Burry wrote:
| Quote: | Ron Natalie wrote:
indrawati.yahya (AT) gmail (DOT) com wrote:
But of course we would never want to return a reference to a temporary,
would we? ;)
It's not a temporary, just a reference to an object whose lifetime
has ended.
You have just now defined temporary -)-)
|
No, I haven't. A temporary object has a precise meaning in C++
which does not include automatic variables. No object in C++
has an infinite lifetime. It behooves you to understand this
and when you can use them. |
|
| Back to top |
|
 |
Sjouke Burry Guest
|
Posted: Mon May 22, 2006 9:21 pm Post subject: Re: Silly returning-an-object question |
|
|
Ron Natalie wrote:
| Quote: | Sjouke Burry wrote:
Ron Natalie wrote:
indrawati.yahya (AT) gmail (DOT) com wrote:
But of course we would never want to return a reference to a temporary,
would we? ;)
It's not a temporary, just a reference to an object whose lifetime
has ended.
You have just now defined temporary -)-)
No, I haven't. A temporary object has a precise meaning in C++
which does not include automatic variables. No object in C++
has an infinite lifetime. It behooves you to understand this
and when you can use them.
You can choose whatever name you want for an |
object which just has died(gone out of scope,
or whatever),as long as you dont use it ,reference
it or treat it as if it still existed.
If you dont call that a temporary existence,
you are welcome to any other definition,just as
long at it means the same.
I have seen to many applications just sort of
working because the reference seemed still to be
valid,depending on what you (or your interrupt
software)did. |
|
| Back to top |
|
 |
Luke Meyers Guest
|
Posted: Tue May 23, 2006 4:21 am Post subject: Re: Silly returning-an-object question |
|
|
Sjouke Burry wrote:
| Quote: | Ron Natalie wrote:
Sjouke Burry wrote:
Ron Natalie wrote:
It's not a temporary, just a reference to an object whose lifetime
has ended.
You have just now defined temporary -)-)
No, I haven't. A temporary object has a precise meaning in C++
which does not include automatic variables. No object in C++
has an infinite lifetime. It behooves you to understand this
and when you can use them.
You can choose whatever name you want for an
object which just has died(gone out of scope,
or whatever),as long as you dont use it ,reference
it or treat it as if it still existed.
If you dont call that a temporary existence,
you are welcome to any other definition,just as
long at it means the same.
|
Sjouke, you're not going to get very far (except into pointless
arguments) by picking fights over terminology, especially when the C++
standard is on one side and you are on the other. The sort of
reasoning you just gave could be used to justify introducing
pointlessly confusing alternate definitions of technical terms which
already possess clear and unambiguous meanings. For example, I could
rationalize using the term "public" to describe private member
functions in code that exists in the public domain, but it would be
perverse and misleading to do so.
The standard specifies different behavior for temporaries, distinct
from other kinds of variables. Obfuscatory and misleading language
based on what you *want* the word to mean only serves to obscure this
distinction, which is a disservice to all.
| Quote: | I have seen to many applications just sort of
working because the reference seemed still to be
valid,depending on what you (or your interrupt
software)did.
|
Nobody's advocating using objects which no longer exist. A thorough
understanding of object lifetime rules and their implications is
crucial to writing good C++, as is clear use of the proper terminology
so you can communicate with your peers and continue to advance your
knowledge.
Luke |
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|