I'm trying to send a pointer to a class function to another class for execution. Here's an example:I get the following compiler error:Code:class B { public: void DoThis(int num, bool (*p2Func)(std::ofstream&)); private: std::ofstream m_fileOut; }; void B::DoThis(int num, bool (*p2Func)(std::ofstream&)) { (*p2Func)(m_fileOut); } class A { public: A() {m_pB = new B();} ~A() {delete m_pB;} bool Serialize(std::ifstream& ar) {return true;} bool Serialize(std::ofstream& ar) {return true;} void DoSomethingElse(); private: B* m_pB; }; void A::DoSomethingElse() { //do some stuff m_pB->DoThis(0,Serialize); }
error C2664: 'DoThis' : cannot convert parameter 2 from 'bool (std::ofstream &)' to 'bool (*)(std::ofstream &)'
None of the functions with this name in scope match the target type
When I try changing the call to DoThis to:I get the following compiler error:Code:m_pB->DoThis(0,&Serialize);
error C2276: '&' : illegal operation on bound member function expression
I'm not exactly sure what I'm doing wrong. Can someone point me in a better direction?



LinkBack URL
About LinkBacks



I ordinarily would use the 'this and pointer-to-member-function' approach, but syntactically the functor approach looks nicer once all the appropriate templates and classes have been created (all necessary information contained in a single object rather than being passed in pieces - nice if the functor will be shuffled around a bit before use), and has the benefit of (perhaps) better swappability with ordinary function pointers for later code reuse.
