C++ Using Class Method as a Function Pointer Type

C++ How to make function pointer to class method

A member function is quite a bit different from an ordinary function, so when you want to point to a member function you need a pointer-to-member-function, not a mere pointer-to-function. The syntax for a pointer-to-member-function includes the class that the member function is a member of:

void (Game::*mptr)();

This defines a pointer-to-member-function named mptr that holds a pointer to a member function of the class Games that takes no arguments and returns nothing. Contrast that with an ordinary function pointer:

void (*ptr)();

This defined a pointer-to-function named ptr that holds a pointer to a function that takes no arguments and returns nothing.

Calling C++ member functions via a function pointer

Read this for detail :

// 1 define a function pointer and initialize to NULL

int (TMyClass::*pt2ConstMember)(float, char, char) const = NULL;

// C++

class TMyClass
{
public:
int DoIt(float a, char b, char c){ cout << "TMyClass::DoIt"<< endl; return a+b+c;};
int DoMore(float a, char b, char c) const
{ cout << "TMyClass::DoMore" << endl; return a-b+c; };

/* more of TMyClass */
};
pt2ConstMember = &TMyClass::DoIt; // note: <pt2Member> may also legally point to &DoMore

// Calling Function using Function Pointer

(*this.*pt2ConstMember)(12, 'a', 'b');

Function pointer to class method as argument

It just doesn't invoke the given method!

The passed pointer to member function has to be called to get in effect.

I assume that the Button is inherited from the ButtonsHandler. Then, for instance, you can call with the pointer to member function with this pointer as follows:

void Button::onClick(POINT pt, void (ButtonsHandler::*clicked)(void)) 
{
if (PtInRect(&textRect, pt) != 0) {
(this->*clicked)();
//^^^^^^^^^^^^^^^^^^
// or
// std::invoke(clicked, this) // need to include <functional> header(Since C++17)
}
}

If both classes are unrelated, you required an instance of ButtonsHandler to call the member function. For example:

ButtonsHandler obj;
(obj.*clicked)();
// or
// std::invoke(clicked, obj) // need to include <functional> header

Function pointer as parameter in a class method declaration

Yep! Just leave out the name.

void sort(bool (*)(int, int));

Calling a function pointer inside the same class

A typo:

typedef int (Scanner:: should be typedef void (Scanner:: since my functions return void (void MatchCameraToTrianglePaintTextureTriangle( void OtherInnerWork()

Can not call function pointer of a struct to a class method

The intent of your code is difficult to understand (in its current form). But I would be happy to solve few issues in your code.

  1. MS class needs to be declared before you reference it the type from s_fd structure definition :
class MS; // forward declaration

typedef struct s_fd {
void(MS::* fct_read) (int);
void(MS::* fct_write) (int);
} t_fd;

class MS
{ ... }

  1. the syntax to assign function pointer is incorrect. You forgot &:
_fdSet[cs].fct_read = &MS::client_read;

  1. fct_read and fct_write are member function pointers. They should be applied on instance of MS class. In case you want to apply them on this object:
if (i < 5) {
auto fptr = _fdSet[i].fct_read;
(this->*fptr)(i);
}

Difficulty in passing function pointer of a class member function

You can't pass a non-static member function pointer as a regular function pointer. Member functions have access to the this pointer, and the way they get that is via an invisible implicit function parameter. You need to have the object on which to call the function, and the function itself, be bound together, which a function pointer simply can't do.

What we can do is make print_array_of_length5 a function template, and allow it to take any type of callable. That would give you something like this:

template <typename Function>
void print_array_of_length5(Function func){
for (int i = 0; i < 5; i++)
printf("%d ", func(i));
}

To call it with a non-static member function, you can use a lambda expression, or std::bind(), like this:

SIMPLE smpl;
print_array_of_length5([&smpl](int foo){ return smpl.retval(foo); });
using namespace std::placeholders;
SIMPLE smpl;
auto func = std::bind(&SIMPLE::retval, &smpl, _1);
print_array_of_length5(func);


Related Topics



Leave a reply



Submit