我有这个代码示例:
class Number
{
int i;
public:
Number(int i1): i(i1) {}
operator int() const {return i;}
};
从强制转换运算符中删除 const
修饰符有什么影响?
它会影响自动转换吗?为什么?
最佳答案
如果转换运算符不是 const,则不能转换 const 对象:
const Number n(5);
int x = n; // error: cannot call non-const conversion operator
关于c++ - 强制转换运算符 - const 与非 const,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2325121/