Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-18-2006, 01:00 PM   #1 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
[TIP] Virtual Destructor

I'ts been a long time ago when I posted a [TIP] for the last time. A [TIP] is a mini-tutorial that takes 5 tot 10 minutes to study.

The Virtual Destructor

Imagine code like:
Code:
BaseClass* Bc = new DerivedClass;
delete Bc;
DerivedClass is a class derived from BaseClass

Question: is DerivedClass destructor called?
Answer: to make sure it does, make the destructor of the base class virtual.
That's all there's to it.

Example code:
Code:
#include <iostream>

using namespace std;

class Base
{
public:
  /* virtual*/ ~Base()
  {
    cout<<"Base Dtor called"<<endl;
  }
};

class Derived : public Base
{
public:
  ~Derived()
  {
    cout<<"Derived Dtor called"<<endl;
  }
};

int main()
{
  Base* Bp = new Derived;
  delete Bp;

  return 0;
}
Compile it, run it.
See the output?
Only the base class destructor is called.
See the main() function? The base class pointer points to a derived class! Now you know when the virtual destructor is becoming relevant.

Now remove the comment-tags from virtual in the base class.
Compile and run it.
See the output?
Now both the destructor of the base class and the derived class are called. That's the way to go.

And that's all folks. This topic isn't close to complete, but it's a clear intro I think.
__________________
Valmont is offline   Reply With Quote
Old 10-18-2006, 01:02 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
And [TIP]s stay sticky for a week and remains open for comments.
Do you, the visitor have a tip? Submit it or post it. We'll make it sticky for a week. The credits will be all yours .

So what's your tip?
__________________
Valmont is offline   Reply With Quote
Old 10-18-2006, 03:05 PM   #3 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 705
DJMaze is on a distinguished road
Maybe it's cool to explain virtual so that people understand what it realy does and why it's so important?
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote
Old 10-20-2006, 05:29 AM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Usually a [TIP] is more of a teaser. Why not for the next [TIP] a teaser on virtual methods? Good idea. Perhaps you'd like to write one. See if you can make it a 10-minute read or something.
__________________
Valmont is offline   Reply With Quote
Old 10-24-2006, 02:35 PM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I have thought about your proposal. If you don't beat me to it, I'll make a virutal [TIP]. So basically your idea is a good one in my opinion.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 03:09 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting