Skip to content

Instantly share code, notes, and snippets.

@bananu7
Created August 8, 2014 10:40
Show Gist options
  • Save bananu7/899740849afd5d2f0b83 to your computer and use it in GitHub Desktop.
Save bananu7/899740849afd5d2f0b83 to your computer and use it in GitHub Desktop.
Virtual destructors
#include <iostream>
#include <memory>
using std::cout;
class Base {
public:
virtual ~Base() = default; // REQUIRED!
};
class Noisy {
public:
Noisy() { cout << "Noisy\n"; }
~Noisy() { cout << "~Noisy\n"; }
};
class Derived : public Base {
std::unique_ptr<Noisy> noisy;
public:
Derived() : noisy(new Noisy()) { }
//virtual ~Derived() = default; // not required
};
int main () {
std::unique_ptr<Base> base { new Derived() };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment