0
template <typename T>
class Base
{
public:
    T value;
    Base(T t) : value(t){};
    virtual T& operator ++() = 0;
    T operator ++(int)
    {
        return operator++();
    }
    void print()
    {
        cout << value << endl;
    }
};

template <typename T>
class Derived : public Base<T>
{
public:
    Derived<T>(T t):Base<T>(t){};
    T &operator++() override
    {
        this->value++;
        return this->value;
    }
};

As you see, I overrided prefix operator ++ in Derived, but when I try

Derived<int> derived(0);
derived++;

Here comes an error : no ‘operator++(int)’ declared for postfix ‘++’ [-fpermissive] derived++;

I tried to call

Derived<int> derived(0);
++derived;
derived.print();

And it works, so there must some problems with the way I implement postfix operator ++, any could tell me the correct way?

Vincent_Bryan
  • 178
  • 10

0 Answers0