Friday, 30 June 2017

c++ - Generic object with common baseclass to std::function

Im trying to create a signal handling class with a Qt inspired interface like this



class MessageConsumer {};

class MessageHandler {
public:
static int Connect(int, MessageConsumer* obj, std::function handler) {

// Add callback to handler
}
};

class A : public MessageConsumer {
public:
void handleIt(void) {
// Do stuff
}
};


int main(void) {
A a;
MessageHandler::Connect(1, &a, &A::handleIt);
return 0;
}


But keep getting the compiler error




main.cpp: In function ”int main()”:
main.cpp:71:46: error: no matching function ”MessageHandler::Connect(int, A*, void (A::)())”
MessageHandler::Connect(1, &a, &A::handleIt);
^
main.cpp:53:5: anm: candidate: static int MessageHandler::Connect(int, MessageConsumer
, std::function)
int MessageHandler::Connect(int, MessageConsumer* obj, std::function handler) {
^~~~~~~~~~~~~~
main.cpp:53:5: anm: no known conversion for argument 3 from ”void (A::*)()” to ”std::function”



Is there a way to solve this error without defining templates, i do not want use templating in the interface, i.e.




MessageHandler::Connect(1, &a, &A::handleIt)

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...