Я не знаю заранее, у меня будет Бла, только Бла.
С языковой точки зрения Blah<T>
не имеет смысла, поскольку T
не существует. В зависимости от того, что именно вы пытаетесь сделать, вы можете
сделать Foo
шаблоном, чтобы вы могли объявить параметр шаблона T
:
template<typename T>
class Foo
{
public:
Foo getInstance(const string& fooType);
...
private:
shared_ptr< FooImpl<T> > m_impl;
};
, который «исправляет» выбор T
при объявлении переменной типа Foo<T>
;
или make FooImpl
явно производные от общей базы:
class FooBase {
// need to define the interface here
};
// this is a class definition whereas previously you only needed a declaration
template<typename T>
class FooImpl: public FooBase {
// definition here
};
class Foo
{
public:
Foo getInstance(const string& fooType);
// we needed the definition of FooImpl for this member
// in addition this member is quite obviously a template
template<typename T>
void
set(FooImpl<T> const& foo)
{
m_impl.reset(new FooImpl<T>(foo));
}
// not a member template!
void
use()
{
// any use of m_impl will be through the FooBase interface
}
private:
shared_ptr<FooBase> m_impl;
};
, где для данного экземпляра Foo
любой тип FooImpl<T>
может быть установлен динамически, а затем использован через интерфейс FooBase
. Это своего рода стирание типа , как его называют в мире C ++.