If overloading the dereferencing operator (operator*), does the member selection operator (operator->) use the overloaded operator or does one need to overload it aswell?
If overloading the dereferencing operator ( operator* ), does the member selection operator (
Share
You will need to overload the arrow operator separately. More generally, even if there is a nice mapping between related operators in C++, if you overload one operator, you do not get the rest overloaded “for free” and must implement them yourself.
That said, it’s easy to implement arrow in terms of dereference:
This works by dereferencing
thisto get a reference to the receiver object, then dereferencing that to invokeoperator *, then taking the address of the returned reference to get a pointer to the object the arrow should be applied to.You may want to look into the Boost.Operators library, which makes it possible to define many logically related operators automatically from a small set of base operations.
Hope ths helps!