Virtual destructors

Can someone elaberate a little on virtual destructors. What happens when you don't use them ect. I skimmed over the tutorial section but couldnt find any information on them. I'm trying to understand what is really happening when using certain features.
It has often been said that when learning C++ a man must also learn a martial art, I highley suggest Google-Fu:

http://en.wikipedia.org/wiki/Virtual_function

So this tells me that until something better roles along, virtuals are default behaviors for those functions.
virtuals are default behaviors for those functions.


Not really...

Try looking for it here: http://www.parashift.com/c++-faq-lite/
First of all let's remember that most people do not have the experiance doing research that we do, and that a little bit of frustration is all it takes to chase you into an inferior language:
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.1

Second, how is this not the same as what I said, conceptually not analytically as we're trying not to fry brains here?
ComputerGeek01: ...until something better roles along, virtuals are default behaviors of those functions.
compared to
www.parashift.com: A virtual function allows derived classes to replace the implementation provided by the base class...
Last edited on
Virtual destructors are not the default behaviors because by default functions are not virtual. I'm not sure what you were trying to get at by saying that, but I had the same reaction as firedraco.

Virtual destructors are not quite the same as other virtual functions. A more on point FAQ Lite link is this one:

http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7

To actually answer the OP's question:

virtual destructors allow you to polymorphically destroy objects. Without making the dtor virtual, attempting to destroy an object through a parent class's pointer could cause severe problems and/or memory leaks.

Here's an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Parent_A {
public:
  ~Parent_A() { cout << "~Parent_A\n"; }
};

class Child_A : public Parent_A {
public:
  ~Child_A() { cout << "~Child_A\n"; }
};

class Parent_B {
public:
  virtual ~Parent_B() { cout << "~Parent_B\n"; }
};

class Child_B : public Parent_B {
public:
  ~Child_B() { cout << "~Child_B\n"; }
};

/*
  Basically, 'A' classes have virtual dtors
  'B' classes do not have virtual dtors
*/

int main()
{
  Parent_A* a = new Child_A;
  Parent_B* b = new Chlid_B;

  // polymorphically destroy the objects
  delete b; // calls ~Child_B, followed by ~Parent_B, as you'd expect
  delete a; // only calls ~Parent_A!  ~Child_A never called!  object not properly destroyed!
}
So can I say if as long as I don't have class inheritance design in my program, I don't need to fret over the keyword virtual ? :P

I like to use class composition over class inheritance as it is easier to debug hahahaa... ya I know I breach the GoF design patterns.
So can I say if as long as I don't have class inheritance design in my program, I don't need to fret over the keyword virtual ? :P


Yes, the virtual keyword is completely useless if you're not using inheritance.

I like to use class composition over class inheritance as it is easier to debug hahahaa


I don't see why it'd be easier to debug =P

Anyway both composition and inheritance have their uses. Inheritance forms an "is a" relationship whereas composition forms a "has a" relationship.

I don't see why it'd be easier to debug =P


Using inheritance, sometimes when you are poring over the subclass methods implementation, it make a call to it's parent. Without using an IDE help, you would think maybe it is calling another method within the subclass instead of it's parent isn't it ?

If you have lot's of such subclasses then you got problem visualizing the whole flow. I use Notepad++ but I believe IDE should help me out but I not IDE fan so go figure.

Using composition, you would see that object.method or object->method signature which is easy to identify isn't it ?

So when I say easier to debug, I am referring to tracing the flow of the code.
Without using an IDE help


Debugging without an IDE is masochism. How do you even use a debugger? Can they even operate without an environment to connect to?

If you have lot's of such subclasses then you got problem visualizing the whole flow.


That's more of a design problem. If the design is good, you won't have any of these problems you're describing. It should be immediately clear what a function call is doing and where it's going. If it isn't, your code is sloppy and/or you need to add comments.

Using composition, you would see that object.method or object->method signature which is easy to identify isn't it ?


I suppose so.
Debugging without an IDE is masochism. How do you even use a debugger? Can they even operate without an environment to connect to?


How do you feel about gdb?
I've never used gdb by itself. I've always used it through another environment (like C::B).

My question above was sincere, not rhetorical. How does it work? How can you set breakpoints, watches, etc if not from an IDE? I can't visualize it.
Last edited on
My question above was sincere, not rhetorical. How does it work? How can you set breakpoints, watches, etc if not from an IDE? I can't visualize it.


Agree totally. gdb is way too old-school and command line based.

For Java I uses Eclipse so I presume I can make it work for my C++ programs too :)

Days without IDE to do debugging is your good old trusted printf and cout isn't it ? But for multi-threading debugging is extremely tricky. I wonder how ppl debug multi-threaded C++ program.
How does it work? How can you set breakpoints, watches, etc if not from an IDE? I can't visualize it.


Basically you type "break" or whatever and a line number, function name, etc. When stepping through it generally shows the line you are on and 2 or 3 lines above/below it. I agree it's quite annoying.
Yes, gdb is actually quite popular. My current company uses it extensively. Most of us there use Emacs or Vi and a number of modified gdbs. No IDE is necessary although they might be nice to have.
Yes, gdb is actually quite popular. My current company uses it extensively. Most of us there use Emacs or Vi and a number of modified gdbs. No IDE is necessary although they might be nice to have.


Let me guess. The average age of your company developers is in the range 35-50 ?

Nowadays, free IDE is rampant and as much as I try to keep away, the GUI effects sway me over bit by bit. Ok not only the GUI but the graphical tools for set breakpoints, watch variables etc etc.

I maybe old but I am willing to adapt new things especially if it is free and good to use :P
Topic archived. No new replies allowed.