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); }
};
海湾合作委员会-墙 说:
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
;也许你可以从第一个函数中删除它(尽管我不想这样做(;也许你可以对this
或to
进行显式类型转换,以强制你喜欢的覆盖。
因为void *get(long i, void* const to)
是const
。
这意味着从tstfn
调用它(这是非常量(将需要从myClass*
到const myClass*
的this
的限定转换,因此调用这两个函数都需要对参数进行转换(this
的处理方式与其他参数相同(,因此调用是不明确的。
仅仅因为你的testfn
是一个非常量函数,它会调用get
的非常量版本。非常量函数get
,取bool
不const void*
。如果只有一个get
函数(可能将void*
作为第二个参数,而不管其恒定性如何(,那么将毫无歧义地调用。
相关文章:
- "Inverse SFINAE"避免模棱两可的过载
- 操作员C++的模棱两可的过载
- 模棱两可的重载模板
- 调用重载的"<大括号括起来的初始值设定项列表>"对于对来说就足够了是模棱两可的
- 模棱两可的 != reverse_iterator运算符
- SFINAE不能防止模棱两可的操作员过载吗?
- C++ 电话号码字母拨号程序 - 语法错别字?还是非 ASCII 字符?
- VSCode 说 std::chrono 是模棱两可的,如果运算符<<重载
- 为什么对模板的调用不模棱两可?
- 修复重载运算符的使用'+'模棱两可?
- 为什么同时覆盖全局新运算符和特定于类的运算符不是模棱两可的行为?
- Antlr4 C++访问模棱两可的分支
- 模棱两可的调用 - 模板化函数
- 在SESHAT中,对"元组"的引用是模棱两可的
- C++17 年与 Clang 的模棱两可的部分专业化
- 电话价格问题(十月挑战代码厨师)
- gcc 中的模棱两可的运算符
- 将 NULL 转换为长不是模棱两可吗?
- 使用 Firebase 从C++获取用户的电话号码
- C++ 为什么电话模棱两可