Skip to content

Instantly share code, notes, and snippets.

@isidore
Last active April 3, 2018 15:48
Show Gist options
  • Save isidore/e42ec06f2937fdf2c34ea61f1cd9bb8d to your computer and use it in GitHub Desktop.
Save isidore/e42ec06f2937fdf2c34ea61f1cd9bb8d to your computer and use it in GitHub Desktop.
#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