c++ - Function pointer to member function -


i'd set function pointer member of class pointer function in same class. reasons why i'm doing complicated.

in example, output "1"

class { public:  int f();  int (*x)(); }  int a::f() {  return 1; }   int main() {  a;  a.x = a.f;  printf("%d\n",a.x()) } 

but fails @ compiling. why?

the syntax wrong. member pointer different type category ordinary pointer. member pointer have used object of class:

class { public:  int f();  int (a::*x)(); // <- declare saying class pointer };  int a::f() {  return 1; }   int main() {  a;  a.x = &a::f; // use :: syntax  printf("%d\n",(a.*(a.x))()); // use object of class } 

a.x not yet on object function called on. says want use pointer stored in object a. prepending a time left operand .* operator tell compiler on object call function on.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -