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

How to Call C Member Function Pointers?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-14 02:47:10975browse

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(&amp;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!

Statement:
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