I was trying to assign a non-static function to a variable in C++.
I have two functions, which are onMessageReceived and enableCommand.
Here are definitions:
NetworkUtil.h:
class NetworkUtil{
public:
std::function<void(char* message)> onMessageReceived;
void enableCommand(char* cmd);
private:
// some private variables
}
NetworkUtil.cpp:
void NetworkUtil::enableCommand(char* cmd) {
if (strcmp(cmd, "INIT_ACK") == 0){
mCreateEnabled = true;
}
else if (strcmp(cmd, "START_ACK")){
mStartEnabled = true;
}
else{
std::cerr << "Unknown Command: " << cmd << std::endl;
}
}
Here is a program that assigns a function to a variable:
void NetworkUtil::onMessage(WebSocket* ws, const WebSocket::Data &data)
{
// not working
onMessageReceived = &(NetworkUtil::enableCommand);
// not working either
onMessageReceived = std::bind(&NetworkUtil::enableCommand, data.bytes);
}
I tried both, but non of them are working. Can I get some advice on assigning a non-static function to a variable?