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.
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:
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.