class Base {
public:
template<typename T>
static Base* construct() {
return new Derived<T>();
}
};
template<typename T>
class Derived : public Base {
public:
Derived() {}
};
That code generates a compile error (VS Studio 2017):
syntax error: unexpected token 'identifier', expected 'type specifier'
Is this invalid C++? Is this pattern not possible?
Declare Base
as:
class Base {
public:
template<typename T>
static Base* construct();
}
Then declare Derived
:
template<typename T>
class Derived : public Base {
public:
Derived() {}
};
Then define Base::construct
:
static Base* Base::construct() {
return new Derived<T>();
}
The compiler needs a full definition of Derived
before it can deal with the new
in Base::construct
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments