C++
  Home arrow C++ arrow Page 4 - Overview of Virtual Functions
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Overview of Virtual Functions
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-06-15

    Table of Contents:
  • Overview of Virtual Functions
  • Abstract class
  • Virtual versus Pure Virtual
  • Virtual Deconstructor
  • Virtual Deconstructor continued

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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().

    More C++ Articles
    More By Gabor Bernat


     

    C++ ARTICLES

    - 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++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 7 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek