Skip to content

Instantly share code, notes, and snippets.

@DarkMio
Created December 24, 2016 11:33
Show Gist options
  • Save DarkMio/3f1e1a38b3dbb672747bd729e5a7582b to your computer and use it in GitHub Desktop.
Save DarkMio/3f1e1a38b3dbb672747bd729e5a7582b to your computer and use it in GitHub Desktop.
class Drawbale {
public:
virtual void draw() const = 0;
};
class A : Drawable {
virtual void draw() const { /*...*/ }
};
class B : Drawable {
virtual void draw() const { /*...*/ }
};
#include "Classes.cpp"
#include <vector>
void main() {
// Example Page 38
A a;
B b;
std::vector<const Drawable&> v;
v.push_back(a);
v.push_back(b);
for(auto d : v) {
d.draw();
}
}
#include "Classes.cpp"
#include <vector>
#include <memory>
void main() {
// smart pointers or raw pointers, idc.
auto a = std::make_unique<A>();
auto b = std::make_unique<B>();
std::vector<std::unique_ptr<Drawable>> v;
v.push_back(a);
v.push_back(b);
for(auto d : v) {
d->draw();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment