Behind The scenes of Classes

Started by
6 comments, last by Great Milenko 22 years ago
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?
The Great Milenko"Don't stick a pretzel up your ass, it might get stuck in there.""Computer Programming is findding the right wrench to hammer in the correct screw."
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...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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*.
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?
The Great Milenko"Don't stick a pretzel up your ass, it might get stuck in there.""Computer Programming is findding the right wrench to hammer in the correct screw."
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.
I''d also would like to know how does GCC does it, so I can finally write Milkshape plugins using Mingw
well at least this thread gave me a clue
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]
The Great Milenko"Don't stick a pretzel up your ass, it might get stuck in there.""Computer Programming is findding the right wrench to hammer in the correct screw."
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
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement