473,804 Members | 2,959 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1842
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

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

Similar topics

9
3193
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 post-increment or post-decrement operator modifies the operand once the entire statement has been executed, not during execution of the statement, so this confused me. An examples given to illustrate the ambiguity is: a = i++; // may increment...
13
6386
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 independent statement i++ and ++i which is more efficient? Regards Kailasam
18
1936
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
3191
by: Angel Tsankov | last post by:
Should pre/post increment/decrement return const or non-const? What about other functions?
1
2267
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 execution of these statments value of y will b 5 while value of z will be 6 WHY? value of x++ and x=x+1is equal it first assign a value of x to y then increment in x. why? expression on the right must be executed first then it must assign value to y...
11
3913
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
9843
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 handled with the same overload, but I just want to make sure.
5
2867
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 std::cout; using std::endl; using std::ostream;
3
3044
by: Stang1 | last post by:
The following statement: line_buf = ' '; is equivalent to: line_buf = ' '; line_len++;
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9571
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10561
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10318
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6845
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.