How is inheritance implemented at the memory level?

前端 未结 5 1109
无人共我
无人共我 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:04

    I don't think the standard makes any guarantees. Compilers can choose to make multiple copies of functions, combine copies that happen to access the same memory offsets on totally different types, etc. Inlining is just one of the more obvious cases of this.

    But most compilers will not generate a copy of the code for A::print to use when called through a C instance. There may be a pointer to A in the compiler's internal symbol table for C, but at runtime you're most likely going to see that:

    A a; C c; a.print(); c.print();
    

    has turned into something much along the lines of:

    A a;
    C c;
    ECX = &a; /* set up 'this' pointer */
    call A::print; 
    ECX = up_cast(&c); /* set up 'this' pointer */
    call A::print;
    

    with both call instructions jumping to the exact same address in code memory.

    Of course, since you've asked the compiler to inline A::print, the code will most likely be copied to every call site (but since it replaces the call A::print, it's not actually adding much to the program size).

提交回复
热议问题