Home >Backend Development >C++ >How to Call C Member Function Pointers?
C Call Pointer To Member Function
Calling pointers to member functions in C requires a unique syntax due to their non-static nature. To invoke such functions effectively, a this pointer representing the object on which the function will be called must be supplied alongside the named parameters.
To specify member function pointers in your code:
typedef void (Box::*HitTest) (int x, int y, int w, int h);
This defines a member function pointer type for the HitTest method of the Box class.
To add member functions to a list:
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); }
To call a pointer to member function:
(box->*h)(xPos, yPos, width, height);
In this example, box represents the this pointer, h is the pointer to the HitTest method, and xPos, yPos, width, and height are the function parameters.
The above is the detailed content of How to Call C Member Function Pointers?. For more information, please follow other related articles on the PHP Chinese website!