Overloading -> (and ->*)

This is a discussion on Overloading -> (and ->*) within the C++ Programming forums, part of the General Programming Boards category; I can't for the life of me figure out the correct way to overload the operators -> and ->*.
I ...

Overloading -> (and ->*)

I can't for the life of me figure out the correct way to overload the operators -> and ->*.

I know that the standard library iterators do it, and that auto_ptr and various other smart pointer classes do it. I can't seem to find any useful documentation on the subject and was hoping someone here might be able to explain how to do this right.

The first point of confusion is the parameter. I would think that the operator would take a member pointer, but that would necessitate overloading for every member function signature and member data type. This seems stupid and I doubt it is correct, but I can't think of anything else.

Secondly, what should this operator return? Should it return anything at all? The return type of the function you invoke? Maybe the data type of the member you access?

Secondly, what should this operator return? Should it return anything at all? The return type of the function you invoke? Maybe the data type of the member you access?

It returns a pointer to an object, upon which the built-in -> operator will act. For example, in a smart pointer implementation, the -> operator just returns the underlying pointer.

In MOST cases your -> operator will simply return a pointer to some underlying object.

EDIT: I suppose I should make this a bit clearer.

operator-> takes NO parameters. Its only purpose is to return a pointer to some object, i.e. the object you are "emulating." In almost all cases where you are overloading ->, it's because you are writing some class B which wraps class A and protects or regulates it somehow (e.g., smart pointers, iterators, etc). So the operator-> will return a pointer to an object of class A, and class B protects or regulates access to this object.

They can be overloaded to do crazy stuff. It is not the compiler that expects certain behavior, it is the users of the class. The users of the class will expect the pointer to be returned and if other stuff happens then the operator won't work with their code. That's why it is generally a good idea to follow common practice when overloading operators. The compiler doesn't care until a user uses it differently than you intend.