简体   繁体   English

我为什么不能在一个班上有这两个重载?

[英]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. 在下面的代码中,如果我注释掉aaabbb ,它将进行编译。 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. 您对aaabbb 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.

相关问题 为什么 std::forward 有两个重载? - Why does std::forward have two overloads? 为什么std :: promise :: set_value()有两个重载 - Why does std::promise::set_value() have two overloads 为什么push_back对左值和右值有两个重载? - Why does push_back have two overloads for lvalues and rvalues? 我在模板 class 中有多个 * 运算符重载。 当我将声明以不同的顺序放置时,为什么会得到不同的结果? - I have multiple overloads of the * operator in a template class. Why do I get different results when I put the declarations in different order? 为什么我不能在异常 class 中有 auto_ptr - Why can't I have an auto_ptr in an Exception class Class 具有两个或更多模板重载 - Class with two or more template overloads 为什么这些超载不明确? - Why aren't these overloads ambiguous? 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载? - Why can't I call any other overloads of the same function while inside a particular function overload in C++? 为什么uint128_t没有uint128_t的移位超载? - Why doesn't uint128_t have bit shift overloads for uint128_t? 如果我为一个类编写运算符new和delete,我是否必须编写所有重载? - If I write operators new and delete for a class, do I have to write all of their overloads?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM