Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- class AbstractBase {
- public:
- virtual ~AbstractBase() = default;
- virtual void f() = 0;
- };
- class C1 : public AbstractBase {
- public:
- void f() override {
- std::cout << "From C1" << std::endl;
- }
- };
- class C2 : public AbstractBase {
- public:
- void f() override {
- std::cout << "From C2" << std::endl;
- }
- };
- AbstractBase& CreateC(int i) {
- static auto c1 = C1();
- static auto c2 = C2();
- if (i == 0) {
- return c1;
- } else {
- return c2;
- }
- }
- int main() {
- AbstractBase& c = CreateC(0);
- c.f();
- c = CreateC(1);
- c.f();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement