 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Steven Cool Guest
|
Posted: Tue Oct 28, 2003 2:14 am Post subject: return references |
|
|
Hello,
A little question about returning references.
I have a method which is returning a reference to a 'Trigger object'.
I call the method like this:
Trigger t = method();
then, I do some changes:
t.setsomething();
t.setsomethingelse();
After this operations, I see that the original object isn't changed.
When I do this:
Trigger& t = method();
t.setsomething();
t.setsomethingelse();
then the object is changed.
Can somebody explain this?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Josephine Schafer Guest
|
Posted: Tue Oct 28, 2003 5:28 am Post subject: Re: return references |
|
|
"Steven Cool" <stevencool007 (AT) hotmail (DOT) com> wrote
| Quote: | Hello,
A little question about returning references.
I have a method which is returning a reference to a 'Trigger object'.
I call the method like this:
Trigger t = method();
|
Here you don't return a reference but a copy of a Trigger object (what method
() has to return).
| Quote: | then, I do some changes:
t.setsomething();
t.setsomethingelse();
After this operations, I see that the original object isn't changed.
When I do this:
Trigger& t = method();
t.setsomething();
t.setsomethingelse();
then the object is changed.
Can somebody explain this?
|
Well that's the way it's supposed to work .
In the first code snippet you make changes to the copy of a Trigger object.
Changes made to a copy don't reflect in the original object.
In the second code snippet you return a reference to a Trigger object probably
created by method ().
Reference is an alias for an object. So any changes made to a reference get
reflected in the object it refers to also.
Also make sure you don't return reference to a local object.
HTH,
J.Schafer
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Artie Gold Guest
|
Posted: Tue Oct 28, 2003 5:29 am Post subject: Re: return references |
|
|
Steven Cool wrote:
| Quote: | Hello,
A little question about returning references.
I have a method which is returning a reference to a 'Trigger object'.
I call the method like this:
Trigger t = method();
|
Here, you've invoked the copy constructor to create a new automatic
object.
| Quote: |
then, I do some changes:
t.setsomething();
t.setsomethingelse();
After this operations, I see that the original object isn't changed.
|
Of course. You've only modified your `local' copy of the object.
| Quote: |
When I do this:
Trigger& t = method();
|
Ah, here you've initialized a reference.
| Quote: | t.setsomething();
t.setsomethingelse();
then the object is changed.
|
Well, naturally. ;-)
HTH,
--ag
--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Erik Max Francis Guest
|
Posted: Tue Oct 28, 2003 3:32 pm Post subject: Re: return references |
|
|
Steven Cool wrote:
| Quote: | I call the method like this:
Trigger t = method();
|
This invokes the copy constructor, which (is likely to) make a copy of
the object.
| Quote: | Trigger& t = method();
|
This does not, so t is the same object as that returned by the call.
--
Erik Max Francis && [email]max (AT) alcyone (DOT) com[/email] && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ It's like being on a diving board that you know is too high.
__/ Sade Adu
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|