You can simply overload operator-> and operator* :
template<class T> class MyClass { T* ptr; public: T* operator->() { return ptr; }
Please note that due to the design decision of the creator of the language, you cannot overload the operator . .
In addition, you might think that operator* will overload the multiplication operator instead of the dereference operator. However, this is not so, because the multiplication operator accepts a single argument (while the dereference operator does not accept arguments), and because of this, the compiler can determine which one.
Finally, note that operator-> returns a pointer, but operator* returns a link. It's easy to confuse them.
source share