[英]Why can't I have these two overloads in a class?
In the code below, if I comment out aaa
or bbb
it will compile. 在下面的代码中,如果我注释掉
aaa
或bbb
,它将进行编译。 Why can't I have both? 为什么我不能同时拥有两者?
#include <iostream>
class MyClass
{
private:
typedef void (MyClass::*aaa)() const;
typedef void (MyClass::*bbb)() const;
void ThisTypeDoesNotSupportComparisons() const {}
public:
operator aaa() const { return (true) ? &MyClass::ThisTypeDoesNotSupportComparisons : 0; }
operator bbb() const { return (true) ? &MyClass::ThisTypeDoesNotSupportComparisons : 0; }
};
int main()
{
MyClass a;
MyClass b;
if(a && b) {}
}
Your typedefs for aaa
and bbb
are identical. 您对
aaa
和bbb
typedef相同。 So your conversion operators are actually declaring the same function. 因此,您的转换运算符实际上是在声明相同的功能。
Essentially, the compiler sees 本质上,编译器会看到
operator void (MyClass::*)() const { ... }
twice, once for aaa
, and once for bbb
. 两次,一次给
aaa
,一次给bbb
。
Because both are type defining the same type with a different name. 因为两者都是用不同的名称定义相同类型的类型。
aaa
is a pointer to a member function of MyClass
that doesn't take any parameters and return a void
. aaa
是指向MyClass
成员函数的指针,该成员函数不接受任何参数并返回void
。
bbb
is also the same thing. bbb
也是一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.