I am using function pointer in my project, facing problem, created a test case to show it... below code fail with below error on MSVC2005 (in simple words i want to access dervied class function through base class function pointer)
error C2440: '=' : cannot convert from 'void (__thiscall ClassB::* )(void)' to 'ClassAFoo'
class ClassA {
public:
virtual void foo()
{
printf("Foo Parent");
}
};
typedef void (ClassA::*ClassAFoo)();
class ClassB : public ClassA {
public:
virtual void foo()
{
printf("Foo Derived");
}
};
int main() {
ClassAFoo fPtr;
fPtr = &ClassB::foo;
}
My questions are
- Is it C++ behavior that I cant access derived class function through a base class function pointer or its a compiler bug?
- I have been playing with above case, if i comment out
ClassB::foo
, this code compile fine, without any further modification, Why is this so, should notfPtr = &ClassB::foo;
again result in compile time error?