|
From: Aggro on 20 Nov 2007 17:56 I have a program, which prints out this: --------- output ----------- Button 1:a Button 2:a --------- output ----------- This is exactly what I want and the program works correctly. The problem is that I can only make the callback functions to work in my derived class (MyWnd) by declaring virtual functions to my base class ( Window ). This is not a very good solutions, since I could end up having several different callback functions which I would rather not have in the Window class. And I would also like to be able to pick any member function name i want to, in the derived classes. So the question is. Can I keep the functionality I now have, without the virtual functions in my base class? I also need to be able to derive other classes from Window and implement similar behaviour to those too. And if I can, how do I do that? -------------- main.cpp ----------------- #include <iostream> class Window; class Button { typedef void (Window::*funcName)( char x ); public: void Connect( Window &wnd, funcName func ) { mwnd = &wnd; mfunc = func; } void Push() { (mwnd->*mfunc)('a'); } Window *mwnd; funcName mfunc; }; class Window { public: virtual void Button1( char x ) { } virtual void Button2( char x ) { } }; class MyWnd : public Window { public: void Button1( char x ) { std::cout << "Button 1:" << x << std::endl; } void Button2( char x ) { std::cout << "Button 2:" << x << std::endl; } void Create( Button *b, Button *b2 ) { b->Connect( *this, &Window::Button1 ); b2->Connect( *this, &Window::Button2 ); } }; int main() { Button *b = new Button(); Button *b2 = new Button(); MyWnd wnd; wnd.Create( b, b2 ); b->Push(); b2->Push(); delete b; delete b2; return 0; } -------------- main.cpp -----------------
|
Pages: 1 Prev: balancing Next: pointer to member function and derivated class |