4

Possible Duplicate:
How to overload the ->* operator?

What is the meaning of operator ->*?

and how it can be useful in overloading ?

Community
  • 1
  • 1
Amol Sharma
  • 1,521
  • 7
  • 20
  • 40
  • See also http://stackoverflow.com/questions/2696864/are-free-operator-overloads-evil . I don't think this is a duplicate of the above, but not a really good question either. – Potatoswatter Jan 03 '12 at 20:11
  • I was going to answer, but here is the jist — `operator->*` defines a binary operator just like `operator+`, `operator*`, etc. It has higher precedence than all the other binary operators so it is useful in forming member accesses, but lower than `operator[]`. It is widely considered to be obscure and may confuse users. – Potatoswatter Jan 03 '12 at 20:18

2 Answers2

1

operator->* is for pointers to members.

struct foo{
  void bar(){}
};

int main(){
  typedef void (foo:*foo_memptr)();
  foo_memptr pfunc = &foo::bar;
  foo f;
  foo* pf = &f;
  (f.*pfunc)(); // on object or reference
  (pf->*pfunc)(); // on pointer to object
}

Overloading it is usually only useful for smart pointers, and even them don't do it because it's really complicated and the same functionality can be achieved by ((*pf).*pfunc)(). Scott Meyers even wrote a PDF on how to do it!

Xeo
  • 129,499
  • 52
  • 291
  • 397
1

The ->* and .* operators are for accessing class members via pointers, see the following link for examples:

http://c-for-crogrammers.org.ua/ch22lev1sec6.html

You may find this SO answer useful as well.

Community
  • 1
  • 1
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306