Venelin
Venelin

Reputation: 3308

Unable to pass function pointer as function param

I would like to pass function pointer as a function parameter.

Here is my code:

void AuthServerOpcodes::ValidateAndSetServerOpcode(ServerOpcode serverOpcode, void(*handlerFunc(std::vector<std::byte> data))) {}

Here is the function I would like to pass as second parameter in ValidateAndSetServerOpcode:

void AuthServerOpcodes::Test(std::vector<std::byte> data) {
    std::cout << "all good" << std:end
}

Here is how I try to pass it:

ValidateAndSetServerOpcode(SMSG_LOGIN_REQUEST, &Test);

However this seems to be not the correct way. When I try to do it in that way I get error:

Cannot initialize a parameter of type 'void (*(*) 
(std::vector<std::byte>))' with an rvalue of type 'void 
(AuthServerOpcodes::*)(std::vector<std::byte>)': different return type 
('void (*)' vs 'void')

Why is that and how can I fix it?

Upvotes: 0

Views: 129

Answers (3)

phuclv
phuclv

Reputation: 41794

Pointers to member must be qualified with the class type, so you need to get the pointer you'll need to use

ValidateAndSetServerOpcode(SMSG_LOGIN_REQUEST, &AuthServerOpcodes::Test);

But it looks like you've tried that in the previous edit, so I guess you've called the function pointer to member incorrectly. You didn't show a minimal, reproducible example so I can't help you more, please create one. Anyway I've created a compiled example on Compiler Explorer

typedef void (AuthServerOpcodes::*HandlerFunc)(std::vector<std::byte> &);

void AuthServerOpcodes::ValidateAndSetServerOpcode(ServerOpcode serverOpcode,
        HandlerFunc handlerFunc)
{
    std::vector<std::byte> myVector;
    (this->*handlerFunc)(myVector);   // call the hander
}

void FreeStandingFunction(AuthServerOpcodes& opc,
    AuthServerOpcodes::HandlerFunc handlerFunc,
    std::vector<std::byte> &data)
{
    (opc.*handlerFunc)(data);
}

As you can see the pointer to member must be called with ->* or .* and the whole dereferencing must be wrapped inside () because those operators has lower precedence than the function call operator ()

See also Function pointer to member function

Some off-topic note:

  • Don't use lines that are too long like that
  • Don't pass vectors by values unless you really need to preserve the outside value. Always pass by reference with const std::vector<>& (or remove const to modify the outside variable)
  • Use '\n' instead of std::endl

Upvotes: 1

oshamash
oshamash

Reputation: 1

It's just a type mismatch, your function is a method of the AccountManager class, so it has this signature similar to: static void Login(AccountManager *this, std::vector<..> data);

You can either detach function from class, change your type definition of handlerFunc or consider different techniques like std::mem_fn or std:bind

https://en.cppreference.com/w/cpp/utility/functional/mem_fn https://en.cppreference.com/w/cpp/utility/functional/bind

Upvotes: 0

Asteroids With Wings
Asteroids With Wings

Reputation: 17454

You can't do that.

There is no function pointer to that function, because it is a member function.

You can instead pass a pointer-to-member-function, or better yet a std::function bound to a lambda that captures the this pointer.

Upvotes: 0

Related Questions