![](/img/trans.png)
[英]How can I overload operator [] for a class containing an array of an abstract class in c++?
[英]How can I overload an operator for an 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&'
谢谢。
在
bool operator > (const CONSTRAINT &rhs)
rhs
是const
。 无法在此方法内更改。 但...
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 。 有关此内容的更多信息以及有关运算符重载的更多其他知识,请参阅运算符重载的基本规则和惯用法是什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.