Home >Backend Development >C++ >How Do I Call Pointers to Member Functions in C ?
Calling Pointer to Member Functions in C
In C , pointers to member functions provide a convenient way to dynamically invoke methods on objects. However, their syntax can be challenging.
Syntax for Calling Pointers to Member Functions
To call a pointer to a member function, the following syntax is used:
(object->*pointer_variable)(params)
where:
Syntax Example
Consider the following 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; (box->*h)(xPos, yPos, width, height); }
In this example, a list of pointers to HitTest member functions is iterated over. Each pointer is fetched from the list and invoked by providing the box pointer as this.
Adding Member Functions to a List of Pointers
To add member functions to a list of pointers, the following syntax can be used:
list.push_back(&box->HitTest);
This code pushes the pointer to the HitTest member function of the box object into the list.
The above is the detailed content of How Do I Call Pointers to Member Functions in C ?. For more information, please follow other related articles on the PHP Chinese website!