简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM