C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: post increment not post

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Robert Swan
Guest





PostPosted: Wed Dec 29, 2004 8:46 pm    Post subject: Re: post increment not post Reply with quote



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





PostPosted: Mon Jan 03, 2005 5:30 pm    Post subject: Re: post increment not post Reply with quote




"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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.