Virtual Destructors

Interview questions often ask about virtual destructors and why they are needed. It’s one of those funny things about C++ that unless you have specifically been shown, you just might not realise you need to know.

So why do you need a virtual destructor?

Essentially, you need a virtual destructor to make sure that ALL the relevant destructors are called when you delete an object via a base class pointer.


Well, that actually sounds far more complicated than it is.

Let’s use an example to make it easier. Imagine you have a base class called Container and a sub class called CardboardBox. You create a CardboardBox, and then use the base class pointer to access it (why? see below). When you’re all done, it’s time to delete your object.

Now, the base class doesn’t do much in the way of clearing up, but the subclass (the cardboard box), can be recycled.

So, when your base class destructor is virtual, deleting the base class pointer will call the sub class destructor without you having to do anything extra – and the box will be recycled as if by magic.

If, however, your base class destructor is not virtual, only the base class destructor will get called and the recycling will not happen. Very bad for the environment.


Let’s see this in action with a code example:

#include <iostream> 
using namespace std;

class Container
	Container() {};
	virtual ~Container()
		cout << "Base class destructor called - not much to do." << endl;

class CardboardBox: public Container {
	CardboardBox() {};
		cout << "Sub class destructor called - let's recycle the cardboard!" << endl;

int main()
	CardboardBox* box = new CardboardBox;

	Container* p = box;

	delete p;

	return 0;

When the destructor is virtual (as it should be), the output of this program is as follows:


You can see that both the sub class and the base class destructors have been called, and you have been a green and conscientious consumer.

However, if you remove the ‘virtual’ keyword from the base class destructor, the output changes to this:


Oh dear. No one recycled their cardboard today.

So you see, making your base class destructors virtual will help keep everything nice and tidy. Which is of course, just how we like it 🙂

Why are you using a base class pointer to access the cardboard box?

This is the key to polymorphism in C++. You can use a base class to interface with objects without knowing what they are. So in this example you can talk to objects via the base class (Container), and you don’t need to specifically know whether they are cardboard boxes, metal boxes, or wicker baskets!

I’ve a fuller explanation of polymorphism here.

C++ Eclipse Masterclass Now Open!


It’s HERE!

This morning at 9am GMT the C++ Eclipse Masterclass opened for enrolment. Hurrah!

I have poured my heart and soul into creating this course, and I am so proud to finally be launching it today.

It opens with an early bird discount which runs until midnight GMT on 1st June, so if you want to grab a bargain, act now!

If you have any questions, or feedback, please do get in touch.

And thank you, to all of my readers, for your ongoing support and enthusiasm. I wouldn’t be doing any of this without you.

atoi and itoa

atoi and iota seem like perfect partners.

atoi is the ‘ascii to integer’ function and itoa is the reverse, the ‘integer to ascii’ function.

You would use atoi to convert a string, say, “23557” to the actual integer 23557.

Similarly, you would use itoa to convert an integer, say 44711, to the equivalent string “44711”.

Very handy.

Now, if you’ve written a lot of code using Microsoft Visual C++ (which is where I first started when I was learning C and C++), you may not be aware of the fact that atoi is part of the ANSI standard, whereas itoa is not. The MSVC compiler supports itoa, so if you are coding on this platform – particularly if you are learning on this platform – then it appears that atoi and itoa are a complementary pair that would be available everywhere. Not so!

Read moreatoi and itoa

Fibonacci | Recursively or Not?

You’re probably all aware of the Fibonacci number sequence. Starting with 0 and 1, the next number is determined by the sum of the previous two numbers, so the sequence begins:

0, 1, 1, 2, 3, 5, 8, 13, 21, …

This is a mathematical concept, and it is defined by a “recurrence relation” (that’s a fancy way of saying that each number (or ‘term’) is generated from a previously calculated value), of:

Fn = Fn-1 + Fn-2

with “seed” or starting values of:

F0 = 0 and F1 = 1.

So, as you can imagine, this seems to lend itself nicely to programmatic recursion. Here’s a little program that will give you the 40th number in the Fibonacci sequence, using recursion:

Read moreFibonacci | Recursively or Not?