Where are member functions stored for an object?

前端 未结 2 890
Happy的楠姐
Happy的楠姐 2020-11-30 06:03

I\'m experimenting with C++ to understand how class/structures and their respective objects are laid out in memory and I understood that each field of a class/structure is a

2条回答
  •  青春惊慌失措
    2020-11-30 06:48

    Member functions or pointers to them aren't stored in the object. (virtual functions are typically called through a pointer stored in a table to which an object has a single pointer to) This would be a huge waste of memory. They're typically stored in a code memory section, and are known to the compiler. The object (*this) is typically passed as an invisible parameter so the functions know on which object to operate when they are called.

    So, in layman terms, you'd have

     0x10001000    void A::foo
     ..........    {code for A::foo}
    

    and

     push a;
     call A::foo (0x10001000)
     pop a;
    

    where a is the object you're calling foo on.

提交回复
热议问题