我正在尝试重载取消引用运算符,但编译以下代码会导致错误 'initializing' : cannot convert from 'X' to 'int'
:
struct X {
void f() {}
int operator*() const { return 5; }
};
int main()
{
X* x = new X;
int t = *x;
delete x;
return -898;
}
我做错了什么?
最佳答案
您应该将取消引用运算符应用于类类型。在你的代码中 x
有一个指针类型。写入以下内容:
int t = **x;
或
int t = x->operator*();
关于c++ - 重载取消引用运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2506085/