Class factory mixin

I would like to create in mixin classthat would pass the classfactory method to the final class of a particular type through several levels of inheritance. More specifically, I would like the factory method to create a new instance of the actual object, which it is called as a member.

So, the class "factory" is inherited by the class A, the class is Ainherited by the class B, I would like to find a way to make B::create()and create an instance B. As far as I can tell, this eliminates the use of a template with a type in the class A, as it B::create()will then create an instance A.

+3
source share
2 answers

, CRTP ? http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern

template <typename T>
struct Mixin
{
    T * create() const { return new T; }
};

class Target : public Mixin<Target>
{
    ...
};
+2

. - , , . .

+1

All Articles