Skip to content

Instantly share code, notes, and snippets.

@AlekSi
Created January 28, 2010 21:01
Show Gist options
  • Save AlekSi/289137 to your computer and use it in GitHub Desktop.
Save AlekSi/289137 to your computer and use it in GitHub Desktop.
#ifndef BASE_H
#define BASE_H
#include "factory.h"
class Base
{
public:
template<class BaseOrDerived>
Base(Factory<BaseOrDerived>* f)
: f_(f)
{ }
private:
const Factory<Base>* f_;
};
#endif // BASE_H
#ifndef DERIVED1_H
#define DERIVED1_H
#include "base.h"
class Derived1 : public Base
{
public:
Derived1(Factory<Derived1>* f) : Base(f) { }
};
#endif // DERIVED1_H
#ifndef FACTORY_H
#define FACTORY_H
template <class BaseOrAnyDerived>
class Factory
{
public:
BaseOrAnyDerived create()
{
return BaseOrAnyDerived(this);
}
};
#endif // FACTORY_H
#include "base.h"
#include "derived1.h"
#include "factory.h"
int main()
{
Factory<Base>().create();
Factory<Derived1>().create();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment