What is & # 8594; * operator in C ++?

C ++ continues to amaze me. Today I learned about the operator → *. It is overloaded, but I do not know how to call it. I manage to overload it in my class, but I don't know what to call it.

struct B { int a; }; struct A { typedef int (A::*a_func)(void); B *p; int a,b,c; A() { a=0; } A(int bb) { b=b; c=b; } int operator + (int a) { return 2; } int operator ->* (a_func a) { return 99; } int operator ->* (int a) { return 94; } int operator * (int a) { return 2; } B* operator -> () { return p; } int ff() { return 4; } }; void main() { A a; A*p = &a; a + 2; } 

edit:

Thanks to the answer. To call an overloaded function, I write

 void main() { A a; A*p = &a; a + 2; a->a; A::a_func f = &A::ff; (&a->*f)(); (a->*f); //this } 
+21
c ++ operator-overloading operator-arrow-star
Nov 22 '09 at 19:22
source share
3 answers

The overloaded operator ->* is a binary operator (while .* Is not overloaded). It is interpreted as a regular binary operator, so in your original case, to call this operator you need to do something like

 A a; B* p = a->*2; // calls A::operator->*(int) 

What you read in Peter's answer relates to the built-in operators, and not to your overloaded ones. What you call in the added example is also a built-in statement, not your overloaded one. To call an overloaded operator, you must do what I do in my example above.

+10
Nov 22 '09 at 19:57
source share

Like .* , ->* used with pointers to elements. There's a whole section on the C ++ FAQ LITE devoted to pointers to elements.

 #include <iostream> struct foo { void bar(void) { std::cout << "foo::bar" << std::endl; } void baz(void) { std::cout << "foo::baz" << std::endl; } }; int main(void) { foo *obj = new foo; void (foo::*ptr)(void); ptr = &foo::bar; (obj->*ptr)(); ptr = &foo::baz; (obj->*ptr)(); return 0; } 
+15
Nov 22 '09 at 19:29
source share

Like any other operator, you can also call it explicitly:

 a.operator->*(2); 
+1
Nov 22 '09 at 22:06
source share



All Articles