473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

post increment not post

I'd like to know why the following program outputs 1, and not 0.

#include <iostream>

class a {
int v;
public:
a():v(0){}
a& operator++(int) {
v++;
}
operator int&() {
return v;
}
};

int main() {
a aa;
// I intend b to initialize b with 0, then increment a
int b = aa++;
std::cout << b << std::endl;
// didn't happen
}

Thanks,
Robert
Jul 22 '05 #1
15 1807
Robert Swan wrote:
I'd like to know why the following program outputs 1, and not 0.
I am surprised it outputs anything. It's not supposed to compile.

#include <iostream>

class a {
int v;
public:
a():v(0){}
a& operator++(int) {
v++;
This function is defined to have no 'return' statement although one
is required for non-void function which is not a c-tor or d-tor.
It is unknown what you _intended_ to do. The program is ill-formed.
}
operator int&() {
return v;
}
};

int main() {
a aa;
// I intend b to initialize b with 0, then increment a
int b = aa++;
std::cout << b << std::endl;
// didn't happen
}


You need to do something about the operator++, then we can talk.

V
Jul 22 '05 #2
Robert Swan wrote:
I'd like to know why the following program outputs 1, and not 0.

#include <iostream>

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;
}
Jul 22 '05 #3
Robert Swan wrote:
operator int&() {
return v;
}


By the way, returning a reference here is probably a bad idea.
Jul 22 '05 #4
Victor Bazarov wrote:
....

I am surprised it outputs anything. It's not supposed to compile.
It did. However, if I had enabled warnings the compiler would have
revealed the inept code.

#include <iostream>

class a {
int v;
public:
a():v(0){}
a& operator++(int) {
v++;

This function is defined to have no 'return' statement although one
is required for non-void function which is not a c-tor or d-tor.
It is unknown what you _intended_ to do. The program is ill-formed.


Oops. I intended to define class a's post operator ++ to increment
member v, then return a reference to the "invokee object" (not sure what
you're supposed to call it).

I was then thinking the statement

int b = aa++;

should be equivalent to

b = aa; a++;

Seems that it's not.

#include <iostream>

class a {
int v;
public:
a():v(0){}
a& operator++(int) {
v++;
return *this;
}
operator int&() {
return v;
}
};

int main() {
a aa;
// I intend to initialize b with 0, then increment a
int b = aa++;
std::cout << b << std::endl;
// didn't happen
}
Jul 22 '05 #5
Ron Natalie wrote:
Robert Swan wrote:
operator int&() {
return v;
}

By the way, returning a reference here is probably a bad idea.


Nah... However, I'd add

operator int() const { return v; }

to boot.

V
Jul 22 '05 #6
Robert Swan wrote:
Victor Bazarov wrote:
...

I am surprised it outputs anything. It's not supposed to compile.

It did. However, if I had enabled warnings the compiler would have
revealed the inept code.


I am not sure what 'inept' is in this case. If your compiler allows
the code to compile, the compiler is non-compliant.
#include <iostream>

class a {
int v;
public:
a():v(0){}
a& operator++(int) {
v++;


This function is defined to have no 'return' statement although one
is required for non-void function which is not a c-tor or d-tor.
It is unknown what you _intended_ to do. The program is ill-formed.

Oops. I intended to define class a's post operator ++ to increment
member v, then return a reference to the "invokee object" (not sure what
you're supposed to call it).


We call it (*this).

I was then thinking the statement

int b = aa++;

should be equivalent to

b = aa; a++;

Seems that it's not.

#include <iostream>

class a {
int v;
public:
a():v(0){}
a& operator++(int) {
v++;
return *this;
}
If the operator++ is written that way, its implementation is not as
intended. The built-in operator++(int) first of all returns an r-value
and second, the value is _before_ the increment. If you make it return
a&, it's not an r-value, and if you 'return *this', by the time the
caller gets the value, the object has already been changed.

See Ron's reply for the "correct" and the intended implementation.
operator int&() {
return v;
}
};

int main() {
a aa;
// I intend to initialize b with 0, then increment a
int b = aa++;
std::cout << b << std::endl;
// didn't happen
}


V
Jul 22 '05 #7
Robert Swan wrote:
I was then thinking the statement

int b = aa++;

should be equivalent to

b = aa; a++;

Seems that it's not.
...


It never is. In your case it is equivalent to

int b = aa.operator++(0);

It will do whatever you do inside your 'a::operator++'. It will
initialize 'b' with whatever you return from your 'a::operator++'.

You want 'b' to have the old "value" of 'aa'? Then it is your
responsibility to preserve and return that old value from
'a::operator++'. You didn't do that, hence the result.

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #8
Victor Bazarov wrote:
Robert Swan wrote:
Victor Bazarov wrote:
...

I am surprised it outputs anything. It's not supposed to compile.


It did. However, if I had enabled warnings the compiler would have
revealed the inept code.

I am not sure what 'inept' is in this case. If your compiler allows
the code to compile, the compiler is non-compliant.


The requirement in the standard isn't that strong. 6.6.3/3: "....
Flowing off the end of a function is equivalent to a return with no
value; this results in undefined behavior in a value-returning
function." Since the behavior is undefined, no diagnostic is required. A
warning is helpful, except that in more complicated functions compilers
often give erroneous warnings.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #9
Pete Becker wrote:
Victor Bazarov wrote:
Robert Swan wrote:
Victor Bazarov wrote:
...
I am surprised it outputs anything. It's not supposed to compile.


It did. However, if I had enabled warnings the compiler would have
revealed the inept code.


I am not sure what 'inept' is in this case. If your compiler allows
the code to compile, the compiler is non-compliant.


The requirement in the standard isn't that strong. 6.6.3/3: "....
Flowing off the end of a function is equivalent to a return with no
value; this results in undefined behavior in a value-returning
function." Since the behavior is undefined, no diagnostic is required. A
warning is helpful, except that in more complicated functions compilers
often give erroneous warnings.


Thank you for the correction, Pete. Not ill-formed, just having undefined
behaviour. Got it.

V
Jul 22 '05 #10
Victor Bazarov wrote:
Ron Natalie wrote:
Robert Swan wrote:
operator int&() {
return v;
}
By the way, returning a reference here is probably a bad idea.


The following use of class a is convenient:

a a_inst;
int& b = a;

So far it's working as expected. Have I created a hazard of some sort?


Nah... However, I'd add

operator int() const { return v; }
I assume this is preventing something bad but I can't think of what it
might be.

to boot.

V

Jul 22 '05 #11
Andrey Tarasevich wrote:
Robert Swan wrote:
I was then thinking the statement

int b = aa++;

should be equivalent to

b = aa; a++;

Seems that it's not.
...

It never is. In your case it is equivalent to

int b = aa.operator++(0);

It will do whatever you do inside your 'a::operator++'. It will
initialize 'b' with whatever you return from your 'a::operator++'.

You want 'b' to have the old "value" of 'aa'? Then it is your
responsibility to preserve and return that old value from
'a::operator++'. You didn't do that, hence the result.


Cool, thanks for the response. I was thinking that operator ++ was
somehow automatically delayed until after all other operations in the
statement. I now see that it's just another operator, and only "post" if
implemented as such.
Jul 22 '05 #12
Robert Swan wrote:
Victor Bazarov wrote:
Ron Natalie wrote:
Robert Swan wrote:

operator int&() {
return v;
}


By the way, returning a reference here is probably a bad idea.

The following use of class a is convenient:

a a_inst;
int& b = a;
You mean,

int& b = a_inst;

don't you? See what happens when classes have such long and convoluted
names?
So far it's working as expected. Have I created a hazard of some sort?


Yes, you created a back door to private data. Now whoever owns 'b' can
change the contents of 'a' without 'a' knowing it. It essentially works
against "data abstraction principle", not that you have to follow it, of
course...
Nah... However, I'd add

operator int() const { return v; }

I assume this is preventing something bad but I can't think of what it
might be.


We need to hear from Ron to understand his objection fully. I can only
guess what his objection was about but I won't.

V
Jul 22 '05 #13

The following use of class a is convenient:

a a_inst;
int& b = a;


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;
Jul 22 '05 #14
Ron Natalie wrote:

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;


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
Jul 22 '05 #15

"Ron Natalie" <ro*@sensor.com> wrote in message
news:41***********************@news.newshosting.co m...
Robert Swan wrote:
I'd like to know why the following program outputs 1, and not 0.

#include <iostream>

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
Jul 22 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Mark Turney | last post by:
I was reading "Practical C++ Programming" yesterday, and it mentioned that the order of execution for post-increment and post-decrement operators was ambiguous. I had previously learned that a...
13
by: kailasam | last post by:
Hello, Iam having a doubt. Which is more efficient post increment or Pre increment? I have read that preincrement is efficient than Post increment. Iam not able to think how it is? For an...
18
by: Patrick Wood | last post by:
I found a problem with C# and post increments. I was going through some source code in c++ and found someone did a post increment: int x=0; for(int i=0; i<10; i++) { x = x++; }
8
by: Angel Tsankov | last post by:
Should pre/post increment/decrement return const or non-const? What about other functions?
1
by: mohsin | last post by:
hi everyone well i m little bit confused about the use of pre an post increment infact my thinking contradict with the logic of programe lets consider an example x=5; y=x++; z=x; after the...
11
by: divya_rathore_ | last post by:
The code: int aaa = 100; printf("%d %d %d\n", --aaa, aaa, aaa--); printf("%d\n", aaa); prints: 99 100 100 98
13
by: jehugaleahsa | last post by:
Hello: In C++, you had to distinguish between post and pre increments when overloading. Could someone give me a short demonstration of how to write these? I get the impression that are...
5
by: simudream | last post by:
//hi maybe helpful others can look at this code and //tell me why the class code won't behave like //intrinsic types with post and pre increment //Version: 1.00 #include <iostream> using...
3
by: Stang1 | last post by:
The following statement: line_buf = ' '; is equivalent to: line_buf = ' '; line_len++;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.