【发布时间】:2017-04-24 19:44:29
【问题描述】:
我有以下代码:
struct CommonVariables
{/*some common variables that need to be proceed*/};
struct CommonHandler
{
static void foo(const CommonVariables& vars) {/*processed vars*/}
void bar(const CommonVariables& vars) {/*processed vars*/}
};
struct AnotherVariables
{/*another variables*/};
struct AnotherHandler
{
static void foo(const AnotherVariables& vars) {/*processed vars*/}
void bar(const AnotherVariables& vars) {/*processed vars*/}
};
struct Derived : CommonHandler, AnotherHandler
{};
当我尝试调用 Derived::foo(/any variable type/) 或 Derived d; d.bar(/任何变量类型/),编译器给我一个错误:“对 'foo(or bar)' 的引用不明确”。
但下面我的情况几乎相同:
struct DifferentHandler
{
static void foo(const CommonVariables& vars) {}
static void foo(const AnotherVariables& vars) {}
void bar(const AnotherVariables& vars) {}
void bar(const CommonVariables& vars) {}
};
调用 DifferentHandler::foo(/any variable type/) 或 'bar' 就可以了。
有很多解决方案可以解决第一个代码块(template traits 等)。我特别想要的是通过继承重载方法。而且我不明白为什么来自 Derived 的调用是模棱两可的(因为输入参数有不同的类型,从编译器的角度来看,这些函数是不同的。就像在 DifferentHandler 中一样)
注意:我在 VS2015 中尝试过 MinGW5.8(在 Qt Creator 中)和 MSVC2015。两个编译器都产生了同样的错误。
【问题讨论】:
-
重复:stackoverflow.com/questions/5365689/… 我不知道如何或没有权限将其标记为这样。
-
@ChristopherPisz,那是您链接的帖子的副本吗?我看不出有什么相似之处。
-
为什么这个问题被否决了?哪些人不喜欢它?
标签: c++ inheritance static-methods