繁体   English   中英

C ++为什么呼叫是暧昧的?

[英]C++ Why the call is ambigous?

class myClass {
   int arr[100];
public:
    void *get(long i, void* const to) const;
    void *get(long i, bool nog);
    void *tstfn(void* const to) { return get(0L,to); }
};

gcc -Wall说:

dt.cpp: In member function ‘void* myClass::tstfn(void*)’:
dt.cpp:6:49: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second: [enabled by default]
dt.cpp:4:9: note: candidate 1: void* myClass::get(long int, void*) const
dt.cpp:5:9: note: candidate 2: void* myClass::get(long int, bool)

两个函数调用都需要类型转换:

  • 调用void*函数需要this添加const qualifer
  • 调用bool函数需要转换to void*bool

因此,通过重载决策规则,两者都不是“更好”的匹配,并且该调用被认为是不明确的。

也许你可以在第二个函数中添加const ; 也许你可以从第一个中删除它(虽然我不愿意); 也许你可以做任何的一个显式类型转换thisto强迫你的首选替代。

因为void *get(long i, void* const to)const

这意味着,从调用它tstfn (这是非常量)将需要用于资格转换thismyClass*const myClass* ,因此调用这两种功能将需要为自变量的转换( this是在相同的方式,其他的参数处理),所以电话是模棱两可的。

只是因为你的testfn是一个非const函数,它会调用get的非const版本。 非const函数get ,取bool而不是const void* 只有一个get函数存在(可能将void*作为第二个参数,不管它的常量),然后将被调用而没有歧义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM