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;
}
What am I doing wrong?
You should apply dereference operator to a class type. In your code
xhas a pointer type. Write the following:or