Does every object of virtual class have a pointer to vtable?

后端 未结 9 933
借酒劲吻你
借酒劲吻你 2020-12-14 10:54

Does every object of virtual class have a pointer to vtable?

Or only the object of base class with virtual function has it?

Where did the vtable stored? code

9条回答
  •  北荒
    北荒 (楼主)
    2020-12-14 11:20

    Try this at home:

    #include 
    struct non_virtual {}; 
    struct has_virtual { virtual void nop() {} }; 
    struct has_virtual_d : public has_virtual { virtual void nop() {} }; 
    
    int main(int argc, char* argv[])
    {
       std::cout << sizeof non_virtual << "\n" 
                 << sizeof has_virtual << "\n" 
                 << sizeof has_virtual_d << "\n";
    }
    

提交回复
热议问题