A vtbl for each class instance ?

Started by
1 comment, last by Cygon 21 years, 8 months ago
Hi! This is not a newbie asking what a pointer or object is. I''m just wondering about how the compiler solves a specific problem of memory layout in classes This could be a followup of the thread When does a class have a vtable ? If I derive some classes from a common base class like this:
  
class Base {
  public:
    virtual void virtualMethod(void) = 0;
    void normalMethod(void) { cout << "normal"; }

  private:
    int variableInBase;
};

class Derived1 : public Base {
  public:
    virtual void virtualMethod(void) { cout << "virtual1"; }
};

class Derived2 : public Base {
  public:
    virtual void virtualMethod(void) { cout << "virtual2"; }

  private:
    int variableInDerived;
}
  
So now I''ve got the following function (which could be sealed in a precompiled library): void callVirtualMethod(Base *pClass) { pClass->virtualMethod(); } If I call this method with a pointer to either Derived1 or Derived2, the compiler would see that the function is expecting a Base * pointer (from the function prototype of course) Now what exactly is passed to the function ? - A pointer to the vtbl of the derived class wouldn''t allow the methods to access the data of the class instance. - A pointer to the data of the class wouldn''t suffice to access the vtbl of the class. So, how is the class instance data connected with the vtbl of the class ? In the thread referenced above, the statement was made that there''s only one vtbl for all instances of a class. If the compiler doesn''t pass two pointers for each class pointer argument, one for the instance data and once for the vtable, then the instance data requires a pointer to the vtbl at least. However, remember that it''s possible (not good practice) to do memset(this, 0, sizeof(*this)). This would overwrite the vtbl pointer with 0 if it wasn''t located before the actual class adress, maybe ? -Markus- P.S. Of course, a lot of this is compiler specific. And such knowledge of the internal workings of a compiler implementation should under no circumstances be relied upon. I''m just curious how a compiler would solve those things
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
Advertisement
quote:Original post by Cygon
- A pointer to the data of the class wouldn''t suffice to access the vtbl of the class.

Incorrect. The data of each instance contains a pointer to the appropriate vtable. This is what makes virtual function calls and RTTI possible. It is also why you shouldn''t clear polymorphic classes with memset, and why polymorphic classes tend to be 4 bytes bigger than you''d think.

(Again, this is talking about a usual implementation. No idea if anywhere does it differently.)


[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]
Ah, so that''s the missing piece.

As i said, the question was only out of curiosity!

Thanks!
-Markus-
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.

This topic is closed to new replies.

Advertisement