Last active
April 3, 2018 15:48
-
-
Save isidore/e42ec06f2937fdf2c34ea61f1cd9bb8d to your computer and use it in GitHub Desktop.
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 "catch.hpp" | |
#include <iostream> | |
class BaseStructure | |
{ | |
public: | |
void(*print)(); | |
}; | |
class One | |
{ | |
public: | |
static void print() | |
{ | |
std::cout << "one"; | |
} | |
static BaseStructure create() | |
{ | |
BaseStructure s; | |
s.print = &One::print; | |
return s; | |
} | |
}; | |
class Two | |
{ | |
public: | |
static void print() | |
{ | |
std::cout << "two"; | |
} | |
static BaseStructure create() | |
{ | |
BaseStructure s; | |
s.print = &Two::print; | |
return s; | |
} | |
}; | |
BaseStructure factory(int i) | |
{ | |
return i == 1 ? One::create() : Two::create(); | |
} | |
TEST_CASE("Factory pattern test") { | |
BaseStructure s = factory(2); | |
s.print(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment