I'm trying to create a sort of factory class in a template. I wanted to do something like a pure virtual function, but it needs to be static as I'm using the function to create types.
What I want to happen is when I declare a class, the template calls the static function. The static function is actually declared in the templatised class.
I've got as far as:
class Base
{
};
template<typename T>
class Type : public Base
{
public:
static void Create()
{
mBase = CreateBase();
}
private:
static Base* CreateBase();
static Base* mBase;
};
class MyType : public Type<MyType>
{
private:
static Base* CreateBase()
{
return new MyType;
}
};
template<typename T>
Base* Type<T>::mBase = NULL;
void test()
{
MyType::Create();
}
I get a link time error:
undefined reference to `Type<MyType>::CreateBase()