How to overload postfix and prefix operators?

If o is an object of a class emp and we want that both the statements o++ and ++o should get properly executed then we must overload both postfix and prefix operators. Following code shows how to overload them.

emp operator++( )

{

// pre incrementation

return *this ;

}

emp operator++ ( int )

{

// post incrementation

return *this ;

}

In the postfix function we must write int in parentheses. It is not a parameter of the function it is only a signal to the compiler to create a postfix version of the operator.

Subscribe / Share

It's very calm over here, why not leave a comment?

Leave a Reply




Categories

Powered by Yahoo! Answers