Method pointers and inheritance
I came across a scenario where I need to register class methods from a subclass, however the calls to Register below (obviously) complains about converting wrong types (CSub:: instead of CBase::).
Code:
class CBase;
typedef void (CBase::*Pointer)();
class CBase
{
public:
void Register(Pointer P);
};
class CSub : public CBase
{
public:
void Method1();
void Method2();
void Method3();
};
CSub Sub;
Sub.Register(&CSub::Method1);
Sub.Register(&CSub::Method2);
Sub.Register(&CSub::Method3);
I could of course use plain functions (static methods) and pass a class objects but that wouldn't be very elegant. Is there a correct way to solve this? What happens if I perform a reinterpret_cast?