polymerphism

Hi,

can anyone please let me know what compiler do in the code below?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Base{
public:
    virtual void funtion1() {
        std::cout << "Base::function1()" << std::endl;
    }

    int i;
};

class Derived : public Base {
public:  
    virtual void 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.

Thanks!



> Or please give some reference

http://books.google.co.in/books?id=hLdmQgAACAAJ
Topic archived. No new replies allowed.