Jump to content
  • Advertisement

Archived

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

Great Milenko

Behind The scenes of Classes

This topic is 5883 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
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
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

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!