OOP - Week 7, Lecture 2: Virtual Destructors
A Zoo That Keeps Changing
In Lecture 1 the animals were ordinary objects, created in main with a name. A real zoo is not like this. Animals arrive while the program is running, and they leave. We do not know how many there are, or which kind comes next, when we write the program. This is the situation of Week 5: the animals must be created on the heap, with new, and each one must be released with delete.
A base-class pointer can hold the address of an object created with new, in the same way as before:
Animal* a = new Dog("Rex"); // allowed: a Dog is an Animal
a->sound(); // Rex says Woof: sound is virtual
The Animal part is built first, and then the Dog part, as in Week 6. But there is a problem when the animal leaves the zoo, and we call delete.
1. The Problem: Deleting Through a Base Pointer
Both classes have a destructor that prints a line, so we can see what runs. Animal has a normal destructor, not virtual:
class Animal {
public:
~Animal(); // not virtual, prints ~Animal
};
class Dog : public Animal {
public:
~Dog(); // prints ~Dog
};
We create a Dog through an Animal* and delete it:
Animal* a = new Dog("Rex");
delete a;
The output is:
Animal Rex
Dog Rex
~Animal Rex
There is no ~Dog Rex. A destructor is chosen like any other non-virtual function: by the type of the pointer, which is Animal*. Only the destructor of Animal runs, and the Dog part of the object is never destroyed. If Dog had created memory with new, as the Student did in Week 5, nobody would release it (a memory leak). In C++, deleting a derived object through a base pointer with a non-virtual destructor is actually undefined behavior, so the program is wrong even if it looks fine.
Some compilers give a warning for this, delete called on non-final 'Animal' that has virtual functions but non-virtual destructor, and some do not.
2. The Fix: A Virtual Destructor
Make the destructor of the base class virtual, in the same way as the functions of Lecture 1:
class Animal {
public:
virtual ~Animal();
};
Now delete a; looks at the real object. It runs the destructor of Dog first, and then the destructor of Animal, as in Week 6, the child first and then the parent:
Animal Rex
Dog Rex
~Dog Rex
~Animal Rex
The destructors of Dog, Cat and Bird are virtual automatically, because they override the virtual destructor of Animal. This is why the derived classes in the example below write only ~Dog();, with no virtual and no override: the word virtual in the base class is enough.
This is different from a function like sound(). There we still write override in the derived class, so the compiler checks that the function matches the virtual function of the base class. A destructor cannot be written with the wrong signature, so there is nothing for override to check.
The rule: if a class is going to be used through base-class pointers, so it has virtual functions, give it a virtual destructor. A destructor that does nothing is fine:
virtual ~Animal() {}is enough to make every derived class safe.
The cost is almost nothing, and the mistake without it is silent, so a base class with virtual functions should always have one.
3. Putting It Together
This is the Animal of Lecture 1, now with constructors and destructors that print a line, and a virtual destructor in Animal. main creates a Dog, a Cat and a Bird with new, keeps them in an array of Animal*, makes each one speak, and deletes them. Click Changes since Lecture 1 to see exactly which lines are new.
#ifndef ANIMAL_H
#define ANIMAL_H
#include <string>
using namespace std;
class Animal {
private:
string name;
public:
Animal(const string& n);
virtual ~Animal();
string getName() const;
virtual void sound() const;
};
#endif#include <iostream>
#include "Animal.h"
using namespace std;
Animal::Animal(const string& n) : name(n) {
cout << "Animal " << name << endl;
}
Animal::~Animal() {
cout << "~Animal " << name << endl;
}
string Animal::getName() const {
return name;
}
void Animal::sound() const {
cout << name << " makes a sound" << endl;
}#ifndef DOG_H
#define DOG_H
#include "Animal.h"
class Dog : public Animal {
public:
Dog(const string& n);
~Dog();
void sound() const override;
};
#endif#include <iostream>
#include "Dog.h"
using namespace std;
Dog::Dog(const string& n) : Animal(n) {
cout << "Dog " << getName() << endl;
}
Dog::~Dog() {
cout << "~Dog " << getName() << endl;
}
void Dog::sound() const {
cout << getName() << " says Woof" << endl;
}#ifndef CAT_H
#define CAT_H
#include "Animal.h"
class Cat : public Animal {
public:
Cat(const string& n);
~Cat();
void sound() const override;
};
#endif#include <iostream>
#include "Cat.h"
using namespace std;
Cat::Cat(const string& n) : Animal(n) {
cout << "Cat " << getName() << endl;
}
Cat::~Cat() {
cout << "~Cat " << getName() << endl;
}
void Cat::sound() const {
cout << getName() << " says Meow" << endl;
}#ifndef BIRD_H
#define BIRD_H
#include "Animal.h"
class Bird : public Animal {
public:
Bird(const string& n);
~Bird();
void sound() const override;
};
#endif#include <iostream>
#include "Bird.h"
using namespace std;
Bird::Bird(const string& n) : Animal(n) {
cout << "Bird " << getName() << endl;
}
Bird::~Bird() {
cout << "~Bird " << getName() << endl;
}
void Bird::sound() const {
cout << getName() << " says Tweet" << endl;
}#include <iostream>
#include "Dog.h"
#include "Cat.h"
#include "Bird.h"
using namespace std;
int main() {
Animal* zoo[3];
zoo[0] = new Dog("Rex");
zoo[1] = new Cat("Tom");
zoo[2] = new Bird("Tweety");
for (int k = 0; k < 3; k++)
zoo[k]->sound();
cout << "the zoo closes" << endl;
for (int k = 0; k < 3; k++)
delete zoo[k];
return 0;
}#ifndef ANIMAL_H
#define ANIMAL_H
#include <string>
using namespace std;
class Animal {
private:
string name;
public:
Animal(const string& n);
string getName() const;
virtual void sound() const;
};
#endif
#include <iostream>
#include "Animal.h"
using namespace std;
Animal::Animal(const string& n) : name(n) {
}
string Animal::getName() const {
return name;
}
void Animal::sound() const {
cout << name << " makes a sound" << endl;
}
#ifndef DOG_H
#define DOG_H
#include "Animal.h"
class Dog : public Animal {
public:
Dog(const string& n);
void sound() const override;
};
#endif
#include <iostream>
#include "Dog.h"
using namespace std;
Dog::Dog(const string& n) : Animal(n) {
}
void Dog::sound() const {
cout << getName() << " says Woof" << endl;
}
#ifndef CAT_H
#define CAT_H
#include "Animal.h"
class Cat : public Animal {
public:
Cat(const string& n);
void sound() const override;
};
#endif
#include <iostream>
#include "Cat.h"
using namespace std;
Cat::Cat(const string& n) : Animal(n) {
}
void Cat::sound() const {
cout << getName() << " says Meow" << endl;
}
#ifndef BIRD_H
#define BIRD_H
#include "Animal.h"
class Bird : public Animal {
public:
Bird(const string& n);
void sound() const override;
};
#endif
#include <iostream>
#include "Bird.h"
using namespace std;
Bird::Bird(const string& n) : Animal(n) {
}
void Bird::sound() const {
cout << getName() << " says Tweet" << endl;
}
#include <iostream>
#include "Dog.h"
#include "Cat.h"
#include "Bird.h"
using namespace std;
void makeNoise(const Animal& a) {
a.sound();
}
int main() {
Animal fish("Nemo");
Dog d("Rex");
Cat c("Tom");
Bird b("Tweety");
fish.sound();
Animal* a = &d;
a->sound();
Animal* zoo[3] = {&d, &c, &b};
for (int k = 0; k < 3; k++)
zoo[k]->sound();
makeNoise(b);
return 0;
}
Click each line of output to see what caused it.