C ++类指针,不提供模板参数 [英] C++ class pointer without giving template parameters
本文介绍了C ++类指针,不提供模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的地狱伙伴叫做互联网。
Hello dear people of the underworld called the internet.
假设我们有一个叫X的类,带有模板参数(Y):
Lets say we have a class called X with the template parameters(Y):
template<class Y>
class X
{
//...
};
我想创建一个没有模板参数的类的实例,然后定义具有模板参数的指针:
And i wanted to create an instance of the class without(not yet) template parameters, and then define the pointer WITH the template parameters:
X* myClass;
//....
myClass = new X<variable>();
这有可能吗?
推荐答案
X不是一个没有模板参数的类型,所以没有,不幸的是。
X is not a type without its template argument so no, unfortunately not. You could achieve what you want if X had a base class which defined the interface you wanted to use though.
例如,
struct Interface
{
Interface() {}
virtual ~Interface(){}
virtual void doSomething() = 0;
};
template <class Y>
class X : public Interface
{
//...
virtual void doSomething() override;
};
std::unique_ptr<Interface> myClass;
//....
myClass.reset(new X<variable>());
myClass->doSomething();
这篇关于C ++类指针,不提供模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文