过载取消引用运算符 [英] Overload dereference operator
本文介绍了过载取消引用运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图重载解除引用操作符,但编译以下代码导致错误'initializing':无法从'X'转换为'int'
:
I'm trying to overload the dereference operator, but compiling the following code results in the error '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;
}
我做错了什么?
推荐答案
您应该将dereference运算符应用于类类型。在您的代码 x
中有一个指针类型。写下:
You should apply dereference operator to a class type. In your code x
has a pointer type. Write the following:
int t = **x;
或
int t = x->operator*();
这篇关于过载取消引用运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文