Last active
March 13, 2019 22:21
-
-
Save t0mmyt/45416ee511bc8988e48990673793791e to your computer and use it in GitHub Desktop.
Notes on Polymorphism in C++
This file contains hidden or 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
class Foo { | |
public: | |
// A purely virtual destructor will prevent this class being instantiated directly | |
virtual ~Foo() = 0; | |
}; | |
class Bar: public Foo { | |
~Bar() override { }; | |
}; |
This file contains hidden or 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 <utility> | |
#include <memory> | |
#include <iostream> | |
#include <vector> | |
using namespace std; | |
// Abstract class that declares a virtual destructor | |
class Engine { | |
public: | |
virtual ~Engine() = 0; | |
string get_name() { return name; }; | |
protected: | |
string name; | |
}; | |
Engine::~Engine() { | |
cout << "Engine (" << name << ") destructed" << endl; | |
} | |
class M123 : public Engine { | |
public: | |
explicit M123(string name) { | |
this->name = move(name); | |
} | |
~M123() override { cout << name << " destructed (M123)" << endl; } | |
}; | |
class M456 : public Engine { | |
public: | |
explicit M456(string name) { | |
this->name = move(name); | |
} | |
~M456() override { cout << name << " destructed (M456)" << endl; } | |
}; | |
void do_something() { | |
cout << ">> The following vector (v1) needs explicit deletion" << endl; | |
auto v1 = vector<Engine *>(); | |
v1.push_back(new M123("e123")); | |
v1.push_back(new M456("e456")); | |
for (auto &e : v1) cout << e->get_name() << endl; | |
for (auto e : v1) delete (e); // <- Explicit destructor calling | |
cout << endl << ">> The following vector (v2) will be cleaned up when the scope if left" << endl; | |
auto v2 = vector<std::unique_ptr<Engine>>(); | |
v2.push_back(std::make_unique<M123>("f123")); | |
v2.push_back(std::make_unique<M456>("f456")); | |
for (auto &i : v2) cout << i->get_name() << endl; | |
cout << ">> ending scope" << endl; | |
} | |
int main() { | |
do_something(); | |
} |
This file contains hidden or 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
>> The following vector (v1) needs explicit deletion | |
e123 | |
e456 | |
e123 destructed (M123) | |
Engine (e123) destructed | |
e456 destructed (M456) | |
Engine (e456) destructed | |
>> The following vector (v2) will be cleaned up when the scope is left | |
f123 | |
f456 | |
>> ending scope | |
f123 destructed (M123) | |
Engine (f123) destructed | |
f456 destructed (M456) | |
Engine (f456) destructed |
This file contains hidden or 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
class Foo { | |
}; | |
class Bar: public Foo { | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment