stl - C++ Unable to convert base class to derived class when registering callback -
i’d create map of callbacks different objects of same hierarchy i'm getting following error:
class responsebase { public: virtual bool parse() = 0; }; class myresponse : public responsebase { public: bool parse() { return true; } void sayhello() {} }; typedef std::tr1::function<void (responsebase*)> mycallback; typedef std::map<int, mycallback> mycallbacks; mycallbacks mycallbacks; void registercallback(int ntype, mycallback mycallback) { mycallbacks::iterator iter = mycallbacks.find(ntype); if (iter == mycallbacks.end()) mycallbacks[ntype] = mycallback; else iter->second = mycallback; } void callback0(responsebase *presponsebase) {} void callback1(myresponse *pmyresponse) {} int main() { registercallback(0, callback0); // ok registercallback(1, callback1); // error c2664: 'void (myresponse *)' : cannot convert parameter 1 'responsebase *' 'myresponse *' return 0; }
but i'd avoid doing this:
void callback1(responsebase *presponsebase) { myresponse *p = static_cast<myresponse*>(presponsebase); p->sayhello(); }
how can work around error?
Comments
Post a Comment