Indeed, in a derived class this postfix increment operator would lead to a sliced object.
struct Base
{
virtual Base& operator++() = 0;
virtual Base operator++(int) = 0;
};
struct Derived : Base
{
void inc();
virtual Base& operator++ () { inc(); return *this; } // no slicing
virtual Base operator++(int) { auto self = *this; inc(); return self; } // slicing
};
What should I do for implementing derived class definition.
My opinion is that a virtual operator is a red flag and a design flaw. Avoid it if possible.
If, "because of homework" you're stuck with it, you could temper with its return type and craft a non-canon postfix operator.
#include <memory>
struct Base
{
virtual Base& operator++() = 0;
virtual std::unique_ptr<Base> operator++(int) = 0;
};
This will come as a great (I mean strong, not good) surprise to the class user:
void f(Derived&);
Derived d;
f(*d++); // this is *weird*