#include <iostream>
class MyInt
{
public:
MyInt(int _int) : m_Int(_int) {}
~MyInt() {}
int& operator* () { returnthis->GetInt(); }
int& GetInt() { return m_Int; }
private:
int m_Int;
};
int Func(MyInt* p)
{
return *p; // this doesn't compile!!!
//return p->operator*(); // ...but this does (and runs as well)
}
int main()
{
MyInt* pInt = new MyInt(5);
int val = Func(pInt);
std::cout << val << std::endl;
return 0;
}
I can't figure out why the overloading doesn't work. It works if I explicitly use the operator. Does the compiler get confused when not explicity using the operator?
What am I doing wrong?
Yes of course. I really didn't have a clear idea about what I was doing. The dereference operator works on a MyInt type, not a pointer to a MyInt as I first thought.