P
PKH
This is a technique that was used in an object-oriented program-architecture
in C that I worked with in the past, and I didn't think was possible to do
something similar for classes in C++. Basically it is a way to send virtual
functions as function-parameters, and is useful in cases where you f.ex have
a class that manages other objects and want to call different functions on
these objects without having to rewrite the traversal code for each call.
This may be obvious to many, but I thought it was cool that it could be done
and maybe someone else will find a use for it also.
The drawback is that you need to use a void* to pass any parameters to the
functions.
here is a simple testprogram to show it:
class BaseClass; // foward declaration of class
typedef void (BaseClass::* BaseClassFunc)(void*); // typedef a
functionpointer for
// BaseClass member function.
// The void* is not used here, but
// is meant for any data that would
// need to be passed
class BaseClass
{
protected:
int
m_int;
public:
BaseClass(){m_int = 0;}
virtual void VirtualFunc(void* pxContext) = 0; // just some meaningless
testfunction
void CallVirtual(BaseClassFunc pF, void*
pxContext){(this->*pF)(pxContext);} // call the parameter-function on self
};
class DerivedClass : public BaseClass
{
public:
void VirtualFunc(void* pxContext){m_int = 1;} // override the VirtualFunc
from the BaseClass
};
int main(int argc, char* argv[])
{
DerivedClass
D;
void*
pxContext = NULL;
/* the following actually calls DerivedClass::VirtualFunc on D, as you
can see by stepping into it. */
/* This function would normally be made for some other class, and contain
the traversal-code */
/* but in this example it is just a member of BaseClass */
D.CallVirtual(BaseClass::VirtualFunc, pxContext);
return 0;
}
in C that I worked with in the past, and I didn't think was possible to do
something similar for classes in C++. Basically it is a way to send virtual
functions as function-parameters, and is useful in cases where you f.ex have
a class that manages other objects and want to call different functions on
these objects without having to rewrite the traversal code for each call.
This may be obvious to many, but I thought it was cool that it could be done
and maybe someone else will find a use for it also.
The drawback is that you need to use a void* to pass any parameters to the
functions.
here is a simple testprogram to show it:
class BaseClass; // foward declaration of class
typedef void (BaseClass::* BaseClassFunc)(void*); // typedef a
functionpointer for
// BaseClass member function.
// The void* is not used here, but
// is meant for any data that would
// need to be passed
class BaseClass
{
protected:
int
m_int;
public:
BaseClass(){m_int = 0;}
virtual void VirtualFunc(void* pxContext) = 0; // just some meaningless
testfunction
void CallVirtual(BaseClassFunc pF, void*
pxContext){(this->*pF)(pxContext);} // call the parameter-function on self
};
class DerivedClass : public BaseClass
{
public:
void VirtualFunc(void* pxContext){m_int = 1;} // override the VirtualFunc
from the BaseClass
};
int main(int argc, char* argv[])
{
DerivedClass
D;
void*
pxContext = NULL;
/* the following actually calls DerivedClass::VirtualFunc on D, as you
can see by stepping into it. */
/* This function would normally be made for some other class, and contain
the traversal-code */
/* but in this example it is just a member of BaseClass */
D.CallVirtual(BaseClass::VirtualFunc, pxContext);
return 0;
}