I cannot compile this code.
From my understanding about virtual function, the compiler should have no problem to de-referencing the f4 in its derived object.
Since the virtual ptr is already in the derived object, I guess even though this object is passed to the base ptr as in the code, when de-referencing f4 in the object, the v-ptr should know the position without problem.
currently I got compilation failed.
Any one can explain it reasonably?
~/private/test>more bv.C
Code:
#include <iostream>
using namespace std;
class base {
private:
public:
base() {}
virtual void f3() { cout<<"base::f3()"<<endl;}
};
class derived: public base {
private:
public:
derived(){}
void f3(){cout<<"derived::f3()"<<endl;}
virtual void f4(){cout<<"derived::f4()"<<endl;}
};
int main(){
base* p = new derived;
p->f4();
return 0;
}
~/private/test>g++ bv.C
bv.C: In function `int main()':
bv.C:24: error: `f4' undeclared (first use this function)
bv.C:24: error: (Each undeclared identifier is reported only once for each
function it appears in.)
~/private/test>