为什么以下代码可以在C++中编译通过?
#include<iostream>
using namespace std;
class mytest
{
public:
operator int()
{
return 10;
}
operator const int()
{
return 5;
}
};
int main()
{
mytest mt;
//int x = mt; //ERROR ambigious
//const int x = mt; //ERROR ambigious
}
为什么允许编译不同版本 (基于constness) 的转换运算符,当它们的使用总是导致模棱两可?有人能澄清我在这里缺少什么吗?
const int() volatile
可能会使其再次变得有用。或许。 - Bo Persson