为什么多重继承具有不同签名的方法是不明确的
Why is multiply inheriting a method with different signatures ambiguous?
如果我从不同基类继承了一个具有相同名称但不同签名的函数,则尝试调用该函数会生成一个错误,声称调用不明确。单个基类中的相同函数不会生成错误。为什么会这样?
第一种情况,http://ideone.com/calH4Q
#include <iostream>
using namespace std;
struct Base1
{
void Foo(double param)
{
cout << "Base1::Foo(double)" << endl;
}
};
struct Base2
{
void Foo(int param)
{
cout << "Base2::Foo(int)" << endl;
}
};
struct Derived : public Base1, public Base2
{
};
int main(int argc, char **argv)
{
Derived d;
d.Foo(1.2);
return 1;
}
prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:27:7: error: request for member ‘Foo’ is ambiguous
prog.cpp:14:10: error: candidates are: void Base2::Foo(int)
prog.cpp:6:10: error: void Base1::Foo(double)
第二种情况,没有错误,http://ideone.com/mQ3J7A
#include <iostream>
using namespace std;
struct Base
{
void Foo(double param)
{
cout << "Base::Foo(double)" << endl;
}
void Foo(int param)
{
cout << "Base::Foo(int)" << endl;
}
};
struct Derived : public Base
{
};
int main(int argc, char **argv)
{
Derived d;
d.Foo(1.2);
return 1;
}
重载发生在相同作用域中定义的名称之间。当多个基定义相同的名称时,这些定义在不同的范围内,因此它们不会重载。如果向Derived
添加两个using声明,则可以将这两个名称拉入Derived
,然后它们将重载。
相关文章:
- Visual C++(VS2017)中用户定义的转换不明确
- 重载类方法的不明确调用
- 为函数定义符号不明确的指针参数
- 方法错误"not all control paths return a value"和方法不返回值
- 父类的私有函数会导致对具有相同名称和相似参数的子类中的公共函数的不明确调用
- GRPC C++ TLS 客户端 grpc::SslCredentials() 方法不返回
- 在 C++17 中的命名空间和子命名空间中重载运算符是不明确的
- C++ 编译器错误:P1LinkedList.cpp:145:错误:重载的"to_string(int&)"调用不明确
- 对重载函数find_first_not_of的不明确调用
- 不明确的成员模板查找
- gcc出现不明确的模板实例化错误
- 使用 nullptr 调用重载方法是不明确的
- 为什么这C++只在编译器上编码一个不明确的方法调用Microsoft?
- 对静态重载(类)函数/方法的调用是不明确的
- 是否有一种方法可以将C 类成员引用到向量而不明确添加它们
- 重写的虚拟方法上的警告不明确
- 对继承的模板方法的不明确调用
- 为什么多重继承具有不同签名的方法是不明确的
- 对重载函数的不明确调用的解决方法
- 为什么这些方法调用不明确