C Calling a Pointer to a Member Function
Calling a pointer to a member function in C requires specific syntax. Here's the anatomy of the syntax:
(*pointer_to_member_function)(object_pointer, arguments);
Example 1: Calling a Member Function with a Pointer
Consider the code snippet:
typedef void (Box::*HitTest) (int x, int y, int w, int h); for (std::list<HitTest>::const_iterator i = hitTestList.begin(); i != hitTestList.end(); ++i) { HitTest h = *i; (*h)(xPos, yPos, width, height); }
Here, hitTestList is a list of pointers to member functions named HitTest. To call each member function using its pointer, you need to specify the this pointer, which refers to the object the function is called on. In this case, you have a pointer to a Box object box, which you can use as the this pointer:
(box->*h)(xPos, yPos, width, height);
Example 2: Adding Member Functions to a List
Consider the code snippet:
std::list<HitTest> list; for (std::list<Box*>::const_iterator i = boxList.begin(); i != boxList.end(); ++i) { Box * box = *i; list.push_back(&box->HitTest); }
Here, you are adding member functions of the Box class to the list list. Note that you are taking the address of the member function using &box->HitTest.
The above is the detailed content of How Do I Call a Pointer to a Member Function in C ?. For more information, please follow other related articles on the PHP Chinese website!