Created
January 28, 2010 21:01
-
-
Save AlekSi/289137 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
#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 |
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
#ifndef DERIVED1_H | |
#define DERIVED1_H | |
#include "base.h" | |
class Derived1 : public Base | |
{ | |
public: | |
Derived1(Factory<Derived1>* f) : Base(f) { } | |
}; | |
#endif // DERIVED1_H |
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
#ifndef FACTORY_H | |
#define FACTORY_H | |
template <class BaseOrAnyDerived> | |
class Factory | |
{ | |
public: | |
BaseOrAnyDerived create() | |
{ | |
return BaseOrAnyDerived(this); | |
} | |
}; | |
#endif // FACTORY_H |
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 "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