Home arrow C++ arrow Page 5 - Overview of Virtual Functions

Overview of Virtual Functions

The word virtual is strange enough on its own. It is defined philosophically as "that which is not real" but contains all of the properties of the real object. However, ultimately something virtual is "fake." Virtual functions are similar in concept, so follow along with me through this article to discover how to use them and when to do so in the C++ language.

Author Info:
By: Gabor Bernat
Rating: 4 stars4 stars4 stars4 stars4 stars / 2
June 15, 2009
  1. · Overview of Virtual Functions
  2. · Abstract class
  3. · Virtual versus Pure Virtual
  4. · Virtual Deconstructor
  5. · Virtual Deconstructor continued

print this article

Overview of Virtual Functions - Virtual Deconstructor continued
(Page 5 of 5 )

When building the application, the compiler also builds a list of all types and saves the information about them. The typeid() keyword is a function that will return a type_info object that holds all of the information about the type. This function has defined the equality operator, so you can simply check to see if two pointers are pointing to the same type of object at run-time.

There is only one requirement. The base object must have at least one virtual function, otherwise a double dereference (leading you to the object itself) will point to the class of the object. However, if you have at least a virtual function over there once, you get the following result:


BananaContainer banana(1, 2, 50);

CarContainer car(2, "Lamborghini Murcielago", 313000);

EmptyContainer empty(3);



std::vector<BaseContainer*> allContainer;





std::vector<BaseContainer*>::iterator it, end;

it = allContainer.begin();

end = allContainer.end(;


if( typeid(**it) == typeid(**it))


//this is always true



for (;it != end; ++it)


cout << endl << typeid(**it).name()<< " " << typeid(**it).raw_name() << endl;

cout << endl << typeid(*it).name() << " " << typeid( *it).raw_name() << endl;





class BananaContainer .?AVBananaContainer@@


class BaseContainer * .PAVBaseContainer@@


class CarContainer .?AVCarContainer@@


class BaseContainer * .PAVBaseContainer@@


class EmptyContainer .?AVEmptyContainer@@


class BaseContainer * .PAVBaseContainer@@

Press any key to continue . . .

Before I finish this, there is just one more thing that I would like to share with you. It is about a method to implement the behavior of the virtual keyword for a compiler. This helps you better understand what work is done under the hood and how this is realized.

For every class that has a virtual function, first construct a virtual table (vtable). This table contains all functions that are virtual and a pointer to the function itself. If that is a pure virtual, then the pointer is NULL.

Let us examine this with the function below.




First we dereference the "it" iterator so we can access the object to which it is pointing. Follow this with a dereference of the object; now we have an appropriate object. This object begins the vtable with a pointer, so dereference that also.

Find our function in the table; this is done by passing through the table via an offset value. Once we have the function, call it via the operator() and execute it.

Here is the code in a downloadable form if you want to play around with it a little. It's written in Visual Studio 2008, but will work under any compiler as long as you make a new project for it and add the files.



Thank you for your attention during these two articles. Small steps like this turn good programmers into very good ones. As always, feel free to take advantage of the comment blog here on the site if you have questions or feedback concerning this article, and our friendly site full of field experts is still up and running under the name of DevHardware. Live with Passion!

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials