class Base{
public:
virtualvoid funtion1() {
std::cout << "Base::function1()" << std::endl;
}
int i;
};
class Derived : public Base {
public:
virtualvoid function1() {
std::cout << "Derived::function1()" << std::endl;
}
int j;
};
int main() {
Base* pBase1 = new Derived();
pBase1->function1();
delete pBase1;
Base* pBase2 = new Base();
pBase2->function1();
delete pBase2;
Derived* pDerived = new Derived();
pDerived->function1();
delete pDerived;
return 0;
}
My question is mainly with pBase1. When Derived class is created and up-cast to base class.
[1]. How _vptr comes to know it's has to call Derived::function1()?
[2]. What actually done by c++ compiler to do this up-cast?
[3]. How data is arranged when Derived class constructor is called?
Is memory model is like this(I am not sure)
1 2 3 4 5 6 7 8 9 10 11
For Base Class:
int j; //addr 1000
_vptr; //addr 1004
For Derived class:
int i; // addr 1000
_vptr; // addr 1004
int j; // addr 1008
_vptr; // addr 1012
Please correct me if I am wrong?
Please explain me how all this things are handled by compiler?
Or please give some reference from where I can get this things.