Home > Backend Development > C++ > How to Call C Member Function Pointers?

How to Call C Member Function Pointers?

Mary-Kate Olsen
Release: 2024-12-14 02:47:10
Original
968 people have browsed it

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);
Copy after login

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(&amp;box->HitTest);
}
Copy after login

To call a pointer to member function:

(box->*h)(xPos, yPos, width, height);
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template