Stef
Stef

Reputation: 41

Overloaded operator in derived class

I am studying Lafore's 4th editing of C++ book and I am stuck with this problem.

I have these two classes, CountDn derives from Counter. In CountDn I want to overload the prefix for decrement operator and the postfix for both increment and decrement.

It works with all the operators except when I try to do ++c11.

I get these errors from the compiler:

50:10: error: no match for 'operator++' (operand type is 'CountDn')

50:10: note: candidate is:

41:13: note: CountDn

CountDn::operator++(int)

41:13: note: candidate expects 1 argument, 0 provided

Even though get_count() works fine I do not understand why the prefix operator does not work.

My thoughts here is if CounterDn class derives from Counter all the functions that are public should be accessible. What can I revise so I can understand the solution for this problem better?

#include <iostream>
using namespace std;

class Counter{
protected:
    unsigned int count;                //count
public:
    Counter() : count(0)               //constructor, no args
    {  }
    Counter(int c) : count(c)          //constructor, one arg
    {  }
    unsigned int get_count() const     //return count
    {
        return count;
    }
    Counter operator ++ ()             //incr count (prefix)
    {
        return Counter(++count);
    }
};


class CountDn : public Counter{
public:
    CountDn() : Counter()              //constructor, no args
    { }
    CountDn(int c): Counter(c)       //constructor, 1 arg
    { }
    CountDn operator -- ()             //decr count (prefix)
    {
        return CountDn(--count);
    }

    CountDn operator --(int){
        return CountDn(count --);
    }
    CountDn operator ++(int)
    {
        return CountDn(count++);
    }
};

int main() {
    CountDn c1(10),c2;
    c2 = ++c1;
    cout << c1.get_count() << endl;
    return 0;
}

Upvotes: 4

Views: 422

Answers (2)

R Sahu
R Sahu

Reputation: 206567

operator++() and operator++(int) are two overloads of the operator++ function.

When the compiler sees the operator++(int) function in the derived class, it does not look for other overloads of the function. Hence, operator++() is not found when trying to compile the line

c2 = ++c1;

Consequently, the pre-increment operator is not found from the base class. You can use the using declaration to bring the pre-increment overload from the base class into the derived class.

class CountDn : public Counter{
  public:

    using Counter::operator++;

    CountDn() : Counter()              //constructor, no args
    { }
    CountDn(int c): Counter(c)       //constructor, 1 arg
    { }
    CountDn operator -- ()             //decr count (prefix)
    {
        return CountDn(--count);
    }

    CountDn operator --(int){
        return CountDn(count --);
    }
    CountDn operator ++(int)
    {
        return CountDn(count++);
    }
};

Now, both the overloads of operator++ are usable for a CountDn object.

However, the following would still be a problem

c2 = ++c1;

since the pre-increment operator returns a Counter object, not a CountDn object. You can use:

++c1;
c2 = c1;

to get around that problem.

Upvotes: 4

Swapnil
Swapnil

Reputation: 1568

CountDn operator ++(int) // For Postfix operator
{
    return CountDn(count++);
}

CountDn operator ++() // For prefix operator
{
    return CountDn(count++);
}

Add both the version prefix operator is with no argument and postfix operator is with argument. You have used the prefix operator in your main function hence need to add the without argument function.

Upvotes: 0

Related Questions