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;
allContainer.push_back(&banana);
allContainer.push_back(&car);
allContainer.push_back(&empty);
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.
(*it)->getPrice();
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. |