[Hugo Ferreira][Positronic Dreams][Stick Soldiers]
"Please Review my WebSite!".
A Problem for the Gurus (maybe)
Edit:
Maybe I misunderstood... function pointers are different:
class foo{
void (*function)(void);
};
void (*mypointerfunc)(void)
mypointerfunc = foo::function;
I think that's what you want.
Use virtual functions,
putting "= 0;" makes it a pure virtual function that MUST be overridden but a deriving class. If you want to make some functions optionally overridable, don't put the equal zero part, then you have to define a function that the base class will call if the deriving class doesn't implement a new version.
___________________________
Freeware development:
ruinedsoft.com
[edited by - iwasbiggs on September 9, 2002 9:17:06 AM]
Maybe I misunderstood... function pointers are different:
class foo{
void (*function)(void);
};
void (*mypointerfunc)(void)
mypointerfunc = foo::function;
I think that's what you want.
Use virtual functions,
putting "= 0;" makes it a pure virtual function that MUST be overridden but a deriving class. If you want to make some functions optionally overridable, don't put the equal zero part, then you have to define a function that the base class will call if the deriving class doesn't implement a new version.
class CRender{ virtual void Resize(int x, int y);};void CRender::Resize(int x, int y){ // do some default action}class NORender : CRender { // resize is inherited, and the base class will be called.};class DXRender : CRender { // resize is overridden and will be handled by the new definition. virtual void Resize(int x, int y);};
___________________________
Freeware development:
ruinedsoft.com
[edited by - iwasbiggs on September 9, 2002 9:17:06 AM]
quote:Original post by pentium3id
I want to, from within a class' method, call another
class' method, using a method pointer.
Your pointer must be declared to be off secondclass type:
void (*CRender::func)(CWindow *);
This makes it difficult to use non-class functions with the same pointer/handler. (The reason why is that non-class methods and member methods have different calling conventions - __stdcall/__cdecl vs __thiscall). You might be able to solve this using binders:
CRender::ResizeWindow( CWindow * wnd, int x, int y ){ wnd->Resize(x, y);}...CRender * r = appropriate_class();wnd.Event_Size = trinary_mem_function( CWindow *, int, int, r );
In the above snippet, trinary_mem_function is a custom adapter that takes the three argument types for a member function and an object off which the member function will be called, allowing you to call the member function as if it were a global function.
Not trivial, not pretty.
The most robust solution I can think of, however, is to use function objects (classes that overload operator ()), because these will make no difference whether they are class members or global objects.
class Resizer{ void operator () (CWindow * wnd, int x, int y) { wnd->Resize(x, y); }}; class SomeClass{private: Resizer myResizer;};
[edited by - Oluseyi on September 9, 2002 8:24:33 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement