• Advertisement

Archived

This topic is now archived and is closed to further replies.

Behind The scenes of Classes

This topic is 5786 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m looking for info on what is happening "behind the scene" so to speak of classes. For example someclass->lpvtbl->somefunction lpvtbl looks to be some sort of struct inside the class that is a list of members that the compiler has generatied. I''d like to find out more on the vtable and any friends it has... anyone got a link?

Share this post


Link to post
Share on other sites
Advertisement
Book: Inside the C++ Object Model



[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...

Share this post


Link to post
Share on other sites
A quick overview:
There isn''t a prescribed (by C++) way for virtual functions to be implemented, just a behavior that they must meet. The behavior is that calls to virtual member functions through a base class pointer must be routed to the virtual functions of the actual concrete class of the pointer.

A usual implementation of this is by using the vtable. A vtable is an array of function pointers, one for each virtual function. This array is _static_, meaning there is only one physical array per class. A class that has virtual functions also has a vtable pointer, pointing to its personal vtable. So, calls to virtual functions go to the vtable indicated by the vtable pointer, and then to the virtual function itself.

For a real life example, let''s say you have two classes, Base and Dervived (which is derived from base). Each has two virtual functions, Foo and Bar. There will be two vtables in static memory (one for Base, one for Derived) with two pointers each; Base''s vtable will have pointers to Base::foo and Base::bar, Derived''s vtable will have pointers to Derived::foo and Derived::bar. These static tables are allocated and filled-in at the very start of the program.

During the course of your program, each time you make a Base object, it initializes the vtable pointer to point to the Base vtable. Created Derived objects similarly intitialize their pointers to point to the Derived vtable.

Any Base* could point to either a Base or a Derived, so if you call foo or bar on any Base*, it actually calls the function pointed-to by that object''s vtable. In this way, either the Base functions or the Derived functions are called through the Base*.

Share this post


Link to post
Share on other sites
furny: I''ll hafta see if I can find that book somewhere....

stoffel:thats interesting, I kinda figured that it the actual implementation of was left up to the compiler you would you happen to have move info on how its implemented in MSVC & GCC would you?

Share this post


Link to post
Share on other sites
It is left up to the compiler (hence my first paragraph). I gave a typical implementation. It happens to be the one MSVC (6.0 at least) uses.

Share this post


Link to post
Share on other sites
hummmm nevermind... playing around with it a bit it wouldn't have worked for what I wanted to do anyways =)

[edited by - Great Milenko on April 17, 2002 9:43:24 PM]

Share this post


Link to post
Share on other sites
That''s known a priori by the compiler, from the class defintions.

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]

[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ ]

Shamelessly ripped from Oluseyi

Share this post


Link to post
Share on other sites

  • Advertisement