I am new to C++, and learning it, while learning operator overloading I came to know we can also overload ->*
, in what cases we need it?

- 32,488
- 9
- 84
- 95
-
1http://stackoverflow.com/questions/8777845/overloading-member-access-operators-c – NPE Apr 12 '13 at 13:38
-
Thanks for down voting it, now can you please explain its purpose and use case i understand +,- operator overloading, but whats the reason behind ->* over loading? – Apr 12 '13 at 13:38
-
Above link does not describe its use case and purpose, or either i dont understand it :(, it mentions about syntax and i am clear for that... – Apr 12 '13 at 13:42
-
@Will Vousden, thanks for making it more clear. – Apr 12 '13 at 13:46
3 Answers
C++ operator overloading is a useful and very strong feature, but it's also one very easy to misuse with catastrophic results.
The rule of thumb is to follow the principle of no surprise. That is, the semantics of your operator overload should match those of the built-in version. (Yes, there are exceptions such as domain-specific languages, boost::spirit etc., but these are isolated affairs).
Let's apply this to ->*
. This operator can normally be invoked with a pointer as left operand and pointer to member as right operand. Therefore, you should only overload it in cases where you want to emulate such semantics. Let's say you're creating a pointer-like class, perhaps a handle or a special smart pointer (when those in std
aren't applicable to your case). You might want to overload ->*
for your class then, to enable it to be used as much as a regular pointer as possible.
Example:
template <typename T>
class LoggingPointer
{
T *ptr_;
std::ostream &log_;
public:
T& operator* () const {
log_ << "Deref";
return *ptr_;
}
T* operator-> () const {
log_ << "Access";
return ptr_;
}
template <typename Mem>
Mem& operator->* (Mem T::*arg) const {
log_ << "Member access";
return ptr_->*arg;
}
};

- 167,307
- 17
- 350
- 455
One place where we use the ->*
operator, is when we're manipulating method pointers.
As a method has to know in which object she is called, we could do:
class A
{
public:
int some_integer;
void add_then_print(int value)
{
this->some_integer = this->some_integer + value;
std::cout << "My integer now: " << this->some_integer;
}
};
typedef void (A::*method_pointer)(int);
int main(void)
{
A* my_first_class_a = new A;
A* my_second_class_a = new A;
method_pointer method = &A::add_then_print;
(my_first_class_a->*(method))(10);
(my_second_class_a->*(method))(12);
return 0;
}
In this case you can see that the add_then_print
method has been saved into method
.
But in order to know what this
represents in add_then_print
, when we call a method using a method pointer, the syntax is as this:
(object->*(method_pointer))(parameters_to_the_method);
In some cases you'd maybe want to overload this operator to do something else than calling it with just the given parameters, or whatever you want.
-
1How does this address the question, which asks about *overloading* `operator ->*`? – Angew is no longer proud of SO Apr 12 '13 at 14:05
-
As mentioned, he's new to C++, before wondering why we would overload the ->* operator, he had to be sure to understand what's the ->* operator stands for. Then he can answer himself if he understands what he's asking. That's my POV, if it's useless i'll delete the answer. – SeedmanJ Apr 12 '13 at 14:11
It's probably not the answer you are looking for, but the only case where you need it is to confuse the hell out of anyone else on the project. And that includes you two weeks down the road. So please don't. Not everything that is possible to do is also a good idea to actually implement.

- 75,013
- 26
- 93
- 142
-
Unless you happen to be implementing a pointer-like class (such as a smart pointer or a special handle), in which case overloading `->*` enables familiar syntax with your class. – Angew is no longer proud of SO Apr 12 '13 at 14:14