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

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
TABLE OF CONTENTS:
  1. · Overview of Virtual Functions
  2. · Abstract class
  3. · Virtual versus Pure Virtual
  4. · Virtual Deconstructor
  5. · Virtual Deconstructor continued

print this article
SEARCH DEVARTICLES

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

You've probably heard from more experienced programmers that all deconstructions should be made virtual. The day arrived to learn why this is so crucial. Until now, you may have used only non-virtual destructors.

If a derived-class object with a non-virtual deconstructor is destroyed explicitly by applying the delete operator to a base-class pointer to the object, the C++ standard specifies that the behavior is undefined.

The derived class is a base object, but also something more. Destroying only the base makes no sense and can have different results depending on the machine on which it runs. Most probably, you will wake up with a few memory leaks. Wasting memory is against the basic principles of good software engineering.

The solution is to append the virtual keyword in the front of the destructor. Do this as high as you can in the class hierarchy, preferably in the main root (base) class. As the rule of virtual functions explains, all destructors in the derived class will also be virtual. It is not necessary to make its derived deconstructors virtual, but this will improve readability; even someone from the outside will be able maintain the code with less effort.

When you delete explicitly with the delete keyword an object using a base pointer to the object, instead of the base pointer's function the derived classes, a deconstructor is responsible for clearing/deleting the object. The constructor will begin from the bottom of the hierarchy, destroy one object, and step further up until it destroys the main base (root) object.

Here is our class at work. As you can see, we can call without fear the getPrice() function, without implementing it in the base, and not worry that it does not exist:

 

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;

for (it = allContainer.begin(), end = allContainer.end();

it != end; ++it)

{

cout << (*it)->getPrice() << endl;

}

 

 

100

313000

0

Press any key to continue . . .

You probably remember the run-time type defining technique we learned in the past article under the name of down casting. Although this is a powerful method, be careful to avoid the cast for each one, and thus lose yourself inside a switch like logic. Today I am going to present another scheme to find out the type of an object at run time. The keyword used is typeid().


blog comments powered by Disqus
C++ ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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