Unary Postfix Operator Overloading

Which of the following statements accurately describe unary postfix operator++ overloading in C++?

A. It can be overloaded with no parameters when the operator function is a class member.

B. It can only be overloaded if the operator function is a class member.

C. It can be overloaded with one parameter when the operator function is free standing function (not a class member).

D. To overload it at least on parameter for the declaration is required.

Questions by dlee6565

Showing Answers 1 - 12 of 12 Answers

AlexeyP

  • Oct 7th, 2009
 

D.

Postfix requires an int parameter. With no parameters, you will be overloading prefix.

Also, postfix should return by value, and of course it has to be a class method and not a freestanding function.

Class Type
{
...
    Type& operator++ ()  // prefix
    {

        Increment(*this); // whatever you want it to mean...

        return *this;   // return the resulting value by reference

    };

    Type operator++ (int)  // dummy  not used

    {

        Type temp = *this;

        ++(*this);   // implement postfix in terms of prefix

        return temp;   // return the original value by value

    }
}

AlexeyP

  • Oct 15th, 2009
 

In the previous,

Increment(*this); // whatever you want it to mean...

 was meant to be pseudocode.

Taken literally, it will pass this object by value and therefore do nothing to it.

I should have written simply

Increment();

ajrobb

  • Sep 22nd, 2010
 

D

A. It can be overloaded with no parameters when the operator function is a class member.

No. As a class member it must have one dummy parameter:

class A {
A operator++(int);
};

B. It can only be overloaded if the operator function is a class member.

No. It can also be a free standing function:

A operator++(A & a, int);

C. It can be overloaded with one parameter when the operator function is free standing function (not a class member).

No. It needs two parameters (see B)

D. To overload it at least on parameter for the declaration is required.

For a class operator, a single dummy parameter is required. For a free standing function, two operators are required.

  Was this answer useful?  Yes

preethi

  • Sep 14th, 2013
 

D

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions