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
Post a Comment