 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Robert Swan Guest
|
Posted: Wed Dec 29, 2004 8:46 pm Post subject: Re: post increment not post |
|
|
Ron Natalie wrote:
| Quote: |
The following use of class a is convenient:
a a_inst;
int& b = a;
|
Yes, I made an error, should be int& b = a_inst;
| Quote: |
But may do the wrong thing, I assume you meant:
int &b = a_inst;
a_inst ++;
a_inst ++;
What does b evaluate to?
or did you really want to let people change the private
member of a_inst by doing:
b = 5;
|
When I reduced my code to create a suitable newsgroup posting I changed
the meaning. The data refered to was not supposed to be private. So if
the only risk is the access to private data then I'm fine. I just wanted
to use the same syntax to initialize a reference from both an int and
another class.
Thanks for your help.
Robert
|
|
| Back to top |
|
 |
Howard Guest
|
Posted: Mon Jan 03, 2005 5:30 pm Post subject: Re: post increment not post |
|
|
"Ron Natalie" <ron (AT) sensor (DOT) com> wrote
| Quote: | Robert Swan wrote:
I'd like to know why the following program outputs 1, and not 0.
#include
class a {
int v;
public:
a():v(0){}
a& operator++(int) {
v++;
You're missing a return here.
If you return
*this
then, the program should behave like you observed.
The v member is incremented and the side effect applied
before the operator++ returns.
If you want it to behave like a real postincrement, you
need to return a copy of the old object.
a operator++(int) {
a temp(*this);
v++;
return a;
|
I take it you meant:
return temp;
?
-Howard
|
|
| 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
|
|