Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Int Argument in operator++

class myClass
{
    public:

    void operator++()
    {
        // ++myInstance.
    }

    void operator++(int)
    {
        // myInstance++.
    }
}

Besides letting the compiler distinguish between myInstance++ and ++myInstance, is the optional int argument in operator++ actually for anything? If so, what is it?

like image 806
Maxpm Avatar asked Jan 11 '11 15:01

Maxpm


People also ask

What is a int operator?

operator int() is a conversion operator, which allows this class to be used in place of an int . If an object of this type is used in a place where an int (or other numerical type) is expected, then this code will be used to get a value of the correct type.

What is the purpose of the int parameter in the operator ++( int function?

The "odd" and unused dummy int argument is used to indicate the odd postfix operators. In other words, in the postfix case, ++ comes between the first (real) operand and the second (dummy) argument and is thus postfix.

Why (+) operator is also called an overloaded operator?

This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. For example, we can overload an operator '+' in a class like String so that we can concatenate two strings by just using +.

Can binary operator be overloaded with more than two arguments?

Binary operators declared as member functions take one argument; if declared as global functions, they take two arguments. If an operator can be used as either a unary or a binary operator (&, *, +, and -), you can overload each use separately. Overloaded operators cannot have default arguments.


2 Answers

As @Konrad said, the int argument is not used for anything, other than to distingush between the pre-increment and post-increment forms.

Note however that your operators should return a value. Pre-increment should return a reference, and post-increment should return by-value. To wit:

class myClass
{

public:

myClass& operator++()
{
    // ++myInstance. 
    return * this;   
}
myClass operator++(int)
{
    // myInstance++.
    myClass orig = *this;
    ++(*this);  // do the actual increment
    return orig;
}
};

EDIT:

As Gene Bushuyev mentions correctly below, it is not an absolute requirement that operator++ return non-void. However, in most cases (I can't think of an exception) you'll need to. Especially if you want to assign the results of the operator to some other value, such as:

myClass a;
myClass x = a++;

EDIT2:

Also, with the postimcrement version, you will return the object before it was incremented. This is typically done using a local temporary. See above.

like image 121
John Dibling Avatar answered Sep 22 '22 06:09

John Dibling


is the optional int argument in operator++ actually for anything?

No. The only purpose is to distinguish between the two overloads. Quite disappointing, I know. ;-)

like image 41
Konrad Rudolph Avatar answered Sep 19 '22 06:09

Konrad Rudolph