Virtual Table layout in memory?

后端 未结 6 1538
陌清茗
陌清茗 2021-02-08 13:11

how are virtual tables stored in memory? their layout?

e.g.

class A{
    public:
         virtual void doSomeWork();
};

class B : public A{
    public:         


        
6条回答
  •  野的像风
    2021-02-08 14:00

    As others already wrote, there is no general approach. (Heck, nobody even mandates that virtual tables are used at all.)

    However, I believe they are most likely implemented as a hidden pointer at a certain offset in the object which references a table of function pointers. Certain virtual functions' addresses occupy certain offsets in that table. Usually there's also a pointer to the dynamic type's std::type_info object.

    If you're interested in things like this, read Lippmann's "Inside the C++ Object Model". However, unless your interest is academic (or you're trying to write a C++ compiler -- but then you shouldn't need to ask), you shouldn't bother. It's an implementation detail you don't need to know and should never rely on.

提交回复
热议问题