MarsJupiter - Family shareware  
 
 Monday, 01 December 2008
The curse of the virtual destructor

C++ is a wonderful language. I could hardly be a bigger fan of inheritance, polymorphism, template and many of the other features of C++.

But all things come at a price and one of those prices tends to be program bloat.

Consider:

class Three{
    long m_test;
public:
     Three(void);
     virtual ~Three(void);

};

class One{
public:
   Three m_three;
    One(void);
     virtual ~One(void);

};

class Two :public One{
public:
    Two(void);
    virtual ~Two(void);
};

class Three {
   long m_test; 
public:
   Three(void);
   virtual ~Three(void);
};

Any instance of class Two will be 12 bytes long, e.g. 4 bytes for the "long" in class Three and 4 bytes each for the virtual function tables for class Three and One and Two.

This is not intended to be a course on C++ but let us remind ourselves of the purpose of the virtual destructor.

If we use polymorphism and have: Two *pTwo = new Two(); One *pOne = pTwo; delete pOne; then without the virtual destructor, the destructor code for class Two will never be called. With resulting memory leaks if class Two allocates any memory.

But here we have two "ifs", if we are using polymorphism and if class Two does allocate any memory. Meanwhile our class uses 8 bytes more memory than it needs to for every instance we allocate.

It is notable, that Microsoft Visual C++ 6.0 makes virtual destructors the default, even for generic classes, In Microsoft Visual C++ 7.00 they have to be selected as an option when you use the class wizard to create a new class.

 
Products

Callisto - Newsreader/Bulletin Board Browser. The best way to
browse usenet and bulletin boards.
Newsreader/BBS Browser


Foboz - Meta Search Engine, The best Meta Search Engine, searching
hundreds of search engines from the best sites across the web.
Meta Search Engine


 

ThereIWas - Intelligent Favorites/History Toolbar for internet
Explorer
Favorites/History Toolbar


 

WhereWasi? - History Word Search Toolbar for Internet Explorer
History Word Search Toolbar

Main Menu
Home
MarsJupiter Products
Callisto Newsreader - News Reader/Forum Browser
Foboz Meta Search Engine - Unbeatable Results
Where Was I?
ThereIWas - intelligent history/favorites
UpFront - Ingres FRS GUI
Company News
Contact Us
FAQ
MarsJupiter Forums
Affiliate Programme
Alex Programming
Resources
The Web Links
The Daily Blat
Newsflash
Current Releases
Callisto News Reader/Bulletin Board Browser 3.14 has now been released:
Download Callisto here
Tucows rating:
Foboz Meta Search Engine 1.52 has been released:
Download Foboz here
Tucows rating:
WhereWAsI? - Internet Explorer history search toolbar 1.50 has been released:
Download WhereWasI? here
AuntyJean - Internet Explorer family friendly Filter toolbar 1.00 has been released:
Download AuntyJean here
ThereIWas- Intelligent Favorites/history Internet Explorer toolbar 1.02 has been released:
Download ThereIWas here
Coop Ad Link Module
Loans|Remortgages|Car Loans|Remortgages|Mobile Phones
 
Go to top of page  Home | MarsJupiter Products | Callisto Newsreader - News Reader/Forum Browser | Foboz Meta Search Engine - Unbeatable Results | Where Was I? | ThereIWas - intelligent history/favorites | UpFront - Ingres FRS GUI | Company News | Contact Us | FAQ | MarsJupiter Forums | Affiliate Programme | Alex Programming | Resources | The Web Links | The Daily Blat |