How is inheritance implemented at the memory level?

前端 未结 5 1112
无人共我
无人共我 2021-02-13 15:36

Suppose I have

class A           { public: void print(){cout<<\"A\"; }};
class B: public A { public: void print(){cout<<\"B\"; }};
class C: public A         


        
5条回答
  •  情深已故
    2021-02-13 16:23

    There will not be any information stored in a object to describe a member function.

    aobject.print();
    bobject.print();
    cobject.print();
    

    The compiler will just convert the above statements to direct call to function print, essentially nothing is stored in a object.

    pseudo assembly instruction will be like below

    00B5A2C3   call        print(006de180)
    

    Since print is member function you would have an additional parameter; this pointer. That will be passes as just every other argument to the function.

提交回复
热议问题