不明确的const函数

时间:2015-12-08 23:19:57

标签: c++ class ambiguous

我使用的api有一个类,其中定义了以下两个函数:

Module *getParent() { return Parent; }
const Module *getParent() const { return Parent; }

当我尝试像这样调用函数的const版本时:

void Foo::bar(Function* F){
    const Module* parent = F->getParent();
}

编译器告诉我函数调用是不明确的。 由于我无法更改函数声明,如何在函数调用中指定我想要的函数版本?

修改:根据要求,确切的错误是:

'getParent' is ambiguous Candidates are: llvm::Module * getParent() const llvm::Module * getParent()

这是文件中唯一的错误。

1 个答案:

答案 0 :(得分:2)

F是指向Function类型对象的指针。由于对象不是const,F->getParent()调用getParent()的非const版本。如果你真的想调用const版本,可以将F作为指向const Function的指针传递,或者将指针转换为指向const对象的指针:const_cast<const Function*>(F)->getParent()将调用getParent()的const版本1}}。