Number of Virtual tables and Virtual Pointers in a C++ Program

后端 未结 6 421
清歌不尽
清歌不尽 2020-12-08 17:20

Let say we have below program:

class A
{     public:
      virtual fun(){};
};
class B:public A
{     public:
     virtual fun(){};
};
int main()
{
     A a1         


        
6条回答
  •  北海茫月
    2020-12-08 18:00

    Note that this is strictly implementation dependent.
    C++ Standard does not talk of vptr or vtable, the virtual mechanism is left out as an implementation detail for compilers. So practically, compilers can implement it without using vptr or vtable.However, almost all known compilers implement it using vptr and vtable.

    Given the above, to answer your question:

    Each class will have its own virtual table.
    While each object has its own virtual pointer.

提交回复
热议问题