Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ increment operator

How to differentiate between overloading the 2 versions of operator ++ ?

const T& operator ++(const T& rhs)

which one?

i++;
++i;
like image 584
Betamoo Avatar asked May 05 '10 23:05

Betamoo


People also ask

What is increment operator in C?

Increment Operators are the unary operators used to increment or add 1 to the operand value. The Increment operand is denoted by the double plus symbol (++). It has two types, Pre Increment and Post Increment Operators.

What is ++ i and i ++ in C?

In C, ++ and -- operators are called increment and decrement operators. They are unary operators needing only one operand. Hence ++ as well as -- operator can appear before or after the operand with same effect. That means both i++ and ++i will be equivalent.

What is increment operator in C++?

Definition. Increment Operator is used to increase the value of the operand by 1 whereas the Decrement Operator is used to decrease the value of the operand by 1. In C++, the value of the variable is increased or decreased by 1 with the help of the Increment operator and the Decrement Operator.

Does C have increment operators?

The increment(++) and decrement operators(--) are important unary operators in C. Unary operators are those which are applied on a single operand. The increment operator increases the value of the variable by one and the decrement operator decreases the value of the variable by one.


1 Answers

For the non-member versions, a function with one parameter is prefix while a function with two parameters and the second being int is postfix:

struct X {};
X& operator++(X&);      // prefix
X  operator++(X&, int); // postfix

For the member-versions, the zero-parameter version is prefix and the one-parameter version taking int is postfix:

struct X {
    X& operator++();    // prefix
    X  operator++(int); // postfix
};

The int parameter to calls of the postfix operators will have value zero.

like image 188
Georg Fritzsche Avatar answered Sep 28 '22 08:09

Georg Fritzsche