Why can't you get a pointer to an overriden method in a derived class?
Here's an example:
Base Class
#ifndef BASE_H
#define BASE_H
class Base
{
typedef void (Base::*BasePointer)( void );
public:
Base(){}
protected:
virtual void FunFunction( void )
{
}
BasePointer pointer;
};
#endif /* BASE_H */
Derived Class
#ifndef DERIVED_H
#define DERIVED_H
#include "Base.h"
class Derived : public Base
{
public:
Derived()
{
pointer = &Base::FunFunction;
}
protected:
// Overrides base function
void FunFunction( void )
{
}
};
#endif /* DERIVED_H */
The constructor in the Derived class attempts to set inherited pointer value equal to the address of the overriden method FunFunction
. But the error that is generated is "error: 'FunFunction' is a protected member of 'Base'". Changing FunFunction's access to public fixes this problem. FunFucntion can't be public however.
Why does it have a problem with
FunFunction
being protected?What is a way to get this desired functionality of this isn't possible?
EDIT
Could a solution involve casting this
to the Base class and then taking the address of the function? Something like &(((Base*)this)::FunFunction);
although that example doesn't work.