如果派生类的对象调用继承到的成员函数,编译器是在基类中还是在派生类中找被调用的函数。
所以继承是相当于复制了一遍代码到派生类中,还是名义上的继承,代码还是存储在基类当中?
初学C++,求解答。。谢谢。
黄舟2017-04-17 13:51:15
There is only one code, compiled into machine code and placed in the code segment, but the address of the function is stored in the virtual table. When calling, the address of the function is taken out from the virtual table and then called.
巴扎黑2017-04-17 13:51:15
From a programmer's point of view, the derived class copies a copy of the functions. From a compiler's point of view, there is only one copy of them (non-virtual, non-static, non-override), which is compiled and placed in the read-only area of the program.