简体   繁体   English

在derrived类中重载运算符

[英]Overloaded operator in derrived class

I am studying Lafore's 4th editing of C++ book and i am stuck with this problem 我正在研究Lafore第四次编辑C ++书籍,我遇到了这个问题

I have these two classes, CountDn derrives from Counter. 我有两个班,来自Counter的CountDn derrives。 In CountDn i want to overload the prefix for decrement operator and the postfix for both increment and decreament. 在CountDn中,我想重载递减运算符的前缀和递增和递减的后缀。 It works with all the operators except when i try to do ++c11. 它适用于所有运算符,除非我尝试使用++ c11。 My compiler gives the following errors: 我的编译器给出以下错误:

50:10: error: no match for 'operator++' (operand type is 'CountDn') 50:10:错误:'operator ++'不匹配(操作数类型为'CountDn')

50:10: note: candidate is: 50:10:注意:候选人是:

41:13: note: CountDn CountDn::operator++(int) 41:13:注意:CountDn CountDn :: operator ++(int)

41:13: note: candidate expects 1 argument, 0 provided 41:13:注意:候选人需要1个参数,0提供

Even though get_count() works fine i do not understand why the prefix operator does not work. 即使get_count()工作正常,我也不明白为什么前缀运算符不起作用。 My thoughts here is if CounterDn class derrives from Counter all the functions that are public should be accessible. 我的想法是,如果CounterDn类来自Counter,那么所有公开的函数都应该是可访问的。 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;
}

operator++() and operator++(int) are two overloads of the operator++ function. operator++()operator++(int)operator++函数的两个重载。

When the compiler sees the operator++(int) function in the derived class, it does not look for other overloads of the function. 当编译器在派生类中看到operator++(int)函数时,它不会查找函数的其他重载。 Hence, operator++() is not found when trying to compile the line 因此,在尝试编译行时找不到operator++()

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. 您可以使用using声明将基类的预增量重载带入派生类。

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. 现在, operator++的重载都可用于CountDn对象。

However, the following would still be a problem 但是,以下仍然是一个问题

c2 = ++c1;

since the pre-increment operator returns a Counter object, not a CountDn object. 因为预增量运算符返回Counter对象,而不是CountDn对象。 You can use: 您可以使用:

++c1;
c2 = c1;

to get around that problem. 解决这个问题。

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. 您在main函数中使用了前缀运算符,因此需要添加无参数函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM