-
-
Save bananu7/899740849afd5d2f0b83 to your computer and use it in GitHub Desktop.
Virtual destructors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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