小编Mel*_* S.的帖子

使用nullptr调用重载方法是不明确的

我有一些overloaed方法,它们采用了一些不同的指针类型.
现在我想将一个特定方法nullptr作为参数调用.

我知道我可以将其nullptr转换为特定类型的指针,我想要它调用的方法.
但我不想/不能投nullptr.

这个例子应该解释我想要做的事情:

class Foo {
    //some attributes
};
class Bar {
    //some attributes
};

void myMethod (Foo*) {
    //I want this method to be called
}
void myMethod (Bar*) {
    //Not this one
}

int main () {
    myMethod(nullptr);              //Something like this
//  myMethod(static_cast<nullptr>); //I don't want to write this.

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我只是用nullptr我得到它来调用它,
error: call of overloaded 'myMethod(std::nullptr_t)' is ambiguous
因为编译器不知道它应该调用哪个方法.

有办法做我想要的吗?
喜欢类似于模板专业化的东西?

c++ overloading nullptr

4
推荐指数
2
解决办法
487
查看次数

标签 统计

c++ ×1

nullptr ×1

overloading ×1