如果我有一个名为 classA 的类,它具有 classB 和 classC 的类型转换重载,我如何禁用 == 运算符以使 classA 被键入为 classC?
class classA{
operator classB();
operator classC();
//Remove this comparison possibility:
operator ==(classC firstObject,classC secondObject);
}
从这个问题开始:
Ambiguous overload when using many typecasts operator overloads
最佳答案
在 C++11 中,您可以 delete方法。
这类似于将方法设为私有(private)。
但是,它表达了程序员的意图并导致更具描述性的编译器错误。
class classA{
operator classB();
operator classC();
//Remove this comparison possibility:
classC operator ==(classC secondObject) = delete;
};
关于C++:删除 == 或特定类型的任何其他运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38031684/