Linked Questions
13 questions linked to/from How can I overload the operator++ in two different ways for postfix a++ and prefix ++a?
2493 votes
10 answers
1.1m views
What are the basic rules and idioms for operator overloading?
Note: The answers were given in a specific order, and have received varying amounts of votes over time. Since the order of answers depends on your answer sorting preferences, here's an index of the ...
-5 votes
1 answer
665 views
Postfix ++ and prefix ++ in operatoroverloading C++, meanings [duplicate]
I've got this code, but I do not understand the output... class Complex { private: float re; float im; public: Complex(float r = 0.0, float i = 0.0) : re(r), im(i){}; ...
0 votes
0 answers
42 views
error operator overloading on class templates [duplicate]
I was trying to overload operators in my template class, but compilator show error: Error C2676 binary '++': 'Iterator' does not define this operator or a conversion to a type acceptable to the ...
925 votes
15 answers
107k views
Why are these constructs using pre and post-increment undefined behavior?
#include <stdio.h> int main(void) { int i = 0; i = i++ + ++i; printf("%d\n", i); // 3 i = 1; i = (i++); printf("%d\n", i); // 2 Should be 1, no ? volatile int u = 0; u ...
24 votes
3 answers
27k views
Prefix/Postfix increment operators
I'm wanting to make sure I understand pass-by-value vs pass-by-reference properly. In particular, I'm looking at the prefix/postfix versions of the increment ++ operator for an object. Let's suppose ...
24 votes
6 answers
13k views
Why does the postfix increment operator take a dummy parameter?
Have a look at these function signatures: class Number { public: Number& operator++ (); // prefix ++ Number operator++ (int); // postfix ++ }; Prefix doesn't take any parameter but ...
10 votes
5 answers
12k views
Do I have to return a reference to the object when overloading a pre-increment operator?
Can I use: MyClass& MyClass::operator++ () { a++; // private var of MyClass return (*this); } Or it can be: MyClass MyClass::operator++ (); What's the difference? Thanks for answers. I ...
2 votes
5 answers
771 views
Operator++: reference vs value return and unused argument
I saw this C++ code as part of a larger example: Date &Date::operator++() { helpIncrement(); return *this; } Date Date::operator++( int ) { Date temp = *this; helpIncrement(); ...
3 votes
1 answer
4k views
c++ postfix / prefix operator overload as non-member function
I am writing my own array class as an exercise. Since, I read non-member functions are actually better in some ways than member functions. (Scott Meyers) I am trying to write as many operator ...
0 votes
2 answers
576 views
why do we have to return const reference from unary prefix operator overloading
I have one doubt on prefix operator overloading . my sample program: class ABC { int i; public: const ABC& operator++() { i=i+1; return *this;} }; int main() { ABC ob ; //let value ...
0 votes
2 answers
155 views
Postfix incrementer overloading for nested enumerated types
I am having difficulty figuring out how to overload the postfix increment operator for an nested enumerated type of class Card. Moreover, I am also having difficulty getting copy assignment to work ...
1 vote
2 answers
112 views
assigning a variable to pre-increment variable and post increment variable?
What's the difference between the following two assignments? #include<iostream> using namespace std; int main(){ int a=10,i=0; ++i = a //COMPILES WITHOUT ERROR i++ = a //GIVES AN ...
-1 votes
1 answer
326 views
C++ operator overloading error: assignment of member" " in read-only object
Error message: class.cpp: In function 'RationalNumber operator++(const RationalNumber&, int)': class.cpp:27:28: error: assignment of member 'RationalNumber::numerator' in read-only object r....