In the code below, if I comment out aaa
or bbb
开发者_如何学编程it will compile. 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. 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
.
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
.
bbb
is also the same thing.
精彩评论