开发者

Why can't I have these two overloads in a class?

开发者 https://www.devze.com 2023-02-20 05:41 出处:网络
In the code below, if I comment out aaa or bbb 开发者_如何学编程it will compile. Why can\'t I have both?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消