如何为抽象 class 重载运算符?
How can I overload an operator for an abstract class?
我是 C++ 的新手,我的教授在学习他的 class 时没有像我希望的那样详细介绍运算符重载。我正在尝试实现一种方法来比较所有继承抽象 class 的对象(使用 > 或 <),但我在使用 syntax/logic.
时遇到了问题
我试着让它成为父 class 的成员,但我不知道如何从基 class 内部调用纯虚函数。然后我尝试使用模板,但这让我很头疼(我的教授也没有深入研究这些模板)。
我知道我在运算符函数方面完全失败了(如果能提供正确语法方面的任何帮助,我们将不胜感激)。
#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
如果程序可以编译。
相反,我收到以下错误:
"the object has type qualifiers that are not compatible with the member function "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
添加到基础 class 方法,而不是派生的 class 方法,是一个很好的例子,说明了这是有用的地方。
由于看起来这段代码正在利用运行时多态性,您可能需要在基础 class 中使用一个虚拟析构函数,以确保在您有一天希望 class 时销毁正确的 classes =25=] 通过指向基 class 的指针派生 class。
总结一下:
#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. For more on that, and much other wisdom on Operator Overloading, see What are the basic rules and idioms for operator overloading?
我是 C++ 的新手,我的教授在学习他的 class 时没有像我希望的那样详细介绍运算符重载。我正在尝试实现一种方法来比较所有继承抽象 class 的对象(使用 > 或 <),但我在使用 syntax/logic.
时遇到了问题我试着让它成为父 class 的成员,但我不知道如何从基 class 内部调用纯虚函数。然后我尝试使用模板,但这让我很头疼(我的教授也没有深入研究这些模板)。
我知道我在运算符函数方面完全失败了(如果能提供正确语法方面的任何帮助,我们将不胜感激)。
#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 如果程序可以编译。
相反,我收到以下错误:
"the object has type qualifiers that are not compatible with the member function "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
添加到基础 class 方法,而不是派生的 class 方法,是一个很好的例子,说明了这是有用的地方。
由于看起来这段代码正在利用运行时多态性,您可能需要在基础 class 中使用一个虚拟析构函数,以确保在您有一天希望 class 时销毁正确的 classes =25=] 通过指向基 class 的指针派生 class。
总结一下:
#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. For more on that, and much other wisdom on Operator Overloading, see What are the basic rules and idioms for operator overloading?