Advertisement

pointers to member functions of a derived class

Started by March 01, 2000 02:57 PM
0 comments, last by Ridcully 24 years, 9 months ago
okay, this is what i am trying to do //we have a base class class CBase {}; //then we derive a class with a public function class CDerived : public CBase { public: void Function(void); } //okay, now we have a external function that takes a //pointer to a function as an argument extern f( void(CBase::*)(void), CBase*); //so what i try to do is give the function f() a pointer //to CDerived''s member function Function f((void(CBase::*)(void))(CDerived::Function)), pDerived); now, this does compile (please don''t mind the syntax, i haven''t checked it), but when function f calls CDerived::Function, the "this" object is shifted by 4bytes! this yields a complete mess of course in CDerived::Function is there a way to pass a pointer to a function of a derived class to another function that actually takes a function pointer to the base class? there must be a work around of some kind, because half-life does that with the SetThink() function... (if anyone of you knows hl ) thanks ridcully
The question isnt very clear to me.
But, does CBase actually have a MyFunc() ?
i.e, is it like this ?

class CBase
{
virtual void MyFunc() {}
};

and then

class CDerived: public CBase
{
void MyFunc() { }
}

CDerived mderived;

in this case you might be able to just cast the "this" pointer to the parent, and pass that functions.
Then you might call the function f() from within CDerived somewhat like this.

f(((CBase*)this).MyFunc(), (CBase*)this);

or

f(((CBase*)pderived).MyFunc(), (CBase*)pderived);

This topic is closed to new replies.

Advertisement