如何重载抽象类的运算符?

时间:2019-07-11 17:13:43

标签: c++ operator-overloading abstract-class

我是C ++的新手,我的教授没有像上课时那样详细介绍运算符重载。我正在尝试实现一种比较对象的方法(使用>或<),这些对象都继承了一个抽象类,但是在语法/逻辑方面遇到了麻烦。

我试图让它成为父类的成员,但是我不知道如何从基类内部调用纯虚函数。然后我尝试使用模板,但这只是让我头疼(我的教授也没有对它们进行太深入的研究。)

我知道我在运算符功能上完全失败了(对使用适当语法的任何帮助将不胜感激)。

#include <iostream>

enum cType { point, maxima, inflection };

class CONSTRAINT {
public:
    //coordinates
    int x, y;
    //gets what type of constraint the object is
    virtual cType getType() = 0; //pure virtual
    //I'm sure this syntax is horrendous and completely wrong.
    //I was just trying to emulate what I found online :(
    bool operator > (const CONSTRAINT &rhs) { 
            //If the constraints have the same type, compare by their x-value
        if (getType() == rhs.getType())
            return (x > rhs.x);
            //Otherwise, it should be point > maxima > inflection
        else
            return (getType() > rhs.getType());
    }
};
class POINT : public CONSTRAINT {
public:
    virtual cType getType() { return point; }
};
class MAXIMA : public CONSTRAINT {
public:
    virtual cType getType() { return maxima; }
};
//I have another inflection class that follows the pattern

int main() {
    POINT point1, point2;
    point1.x = 3;
    point2.x = 5;
    MAXIMA maxima;
    maxima.x = 4;
    std::cout << (point1 > point2);
    std::cout << (point2 > point1);
    std::cout << (maxima > point2);
    std::cout << (point1 > maxima );
    return 0;
}

我希望:0110 该程序是否可以编译。

相反,出现以下错误:

“对象的类型限定符与成员函数” CONSTRAINT :: getType“不兼容”

“'cType CONSTRAINT :: getType(void)':无法将'this'指针从'const CONSTRAINT'转换为'CONSTRAINT&'”

谢谢。

1 个答案:

答案 0 :(得分:3)

bool operator > (const CONSTRAINT &rhs)

rhsconst。无法在此方法内更改。但是...

virtual cType getType() = 0; //pure virtual

不是const方法。这意味着该方法可以更改rhs,因此编译器拒绝允许调用它。

解决方案:声明方法const

virtual cType getType() const = 0; //pure virtual

现在,向编译器承诺不允许调用函数rhs进行更改。如果getType的实现尝试更改对其调用的对象,则编译器还将强制执行此操作,并拒绝编译程序。

旁注:

一旦方法被声明为virtual,所有覆盖也将为virtual

如果应重写方法而不是由于不匹配而导致的错误,override关键字将捕获错误。在基础类方法中添加const而不是派生类方法是一个很好的例子。

由于该代码似乎利用了运行时多态性,因此如果您有一天希望通过指向delete的派生类的指针,则可能需要基类中的虚拟析构函数来确保销毁了正确的类。基类。

将所有内容打包起来

#include <iostream>

enum cType { point, maxima, inflection };

class CONSTRAINT {
public:
    //coordinates
    int x, y;

    virtual ~CONSTRAINT() = default;
//  ^ added

    //gets what type of constraint the object is
    virtual cType getType() const = 0; //pure virtual
//                          ^ added
    //I'm sure this syntax is horrendous and completely wrong.
    //I was just trying to emulate what I found online :(
    bool operator > (const CONSTRAINT &rhs) {
            //If the constraints have the same type, compare by their x-value
        if (getType() == rhs.getType())
            return (x > rhs.x);
            //Otherwise, it should be point > maxima > inflection
        else
            return (getType() > rhs.getType());
    }
};
class POINT : public CONSTRAINT {
public:
    cType getType() const     override { return point; }
//                  ^ added   ^ added
};
class MAXIMA : public CONSTRAINT {
public:
    cType getType() const     override { return maxima; }
//                  ^ added   ^ added
};
//I have another inflection class that follows the pattern

int main() {
    POINT point1, point2;
    point1.x = 3;
    point2.x = 5;
    MAXIMA maxima;
    maxima.x = 4;
    std::cout << std::boolalpha // < added. prints true and false instead of 1 and 0
              << (point1 > point2) << '\n'
              << (point2 > point1) << '\n'
              << (maxima > point2) << '\n'
              << (point1 > maxima);
    // took advantage of chaining and added newlines to the output for clarity
    return 0;
}

最后的注释:通常建议将operator<实现为Free Function。有关更多信息以及有关运算符重载的更多其他知识,请参见What are the basic rules and idioms for operator overloading?