如何继承模板化的基类?
我是STL新手。我已经编写了一个模板基类,如下所示
template <class T>
class Base
{
public:
//Constructor and Destructor ..
Base();
virtual ~Base();
virtual foo() = 0;
};
现在,我想设计我的框架,以便我的继承类将从此类公开派生,并在各自的实现中实现 foo 。问题是我不知道如何从模板基类继承? 是如下所示...
template class<T>
class Derived : public Base<T>
{
// Implementation of Derived constructor etc and methods ...
};
还是正常的 C++ 方式
class Derived : public Base
{
};
有什么建议吗?另外,如果能为像我这样的新手提供有关 STL 入门的任何信息,我将不胜感激......
问候,
阿图尔
I am new to STL. I have written a Template Base class as follows
template <class T>
class Base
{
public:
//Constructor and Destructor ..
Base();
virtual ~Base();
virtual foo() = 0;
};
Now , I want to design my framework such that my inherited classes will be publicly derived from this class and will implement foo in their respective implementations. Problem is that I don't know how to inherit from a Template Base class ?
Is it as below ...
template class<T>
class Derived : public Base<T>
{
// Implementation of Derived constructor etc and methods ...
};
or Normal C++ way
class Derived : public Base
{
};
Any suggestions ? Also, I would appreciate for any information for getting started with STL for newbies like me ...
Regards,
Atul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
后者
将不起作用,因为
Base
不是一个类,它是一个类模板。这取决于Derived
是否应该是一个模板,或者它是否应该继承Base
的特定实例:Template:
非模板:
(其中
Something
是一些具体类型,例如int
或char *
或std::string
)< /p>The later,
will not work, because
Base
is not a class, it's a class template. Than it depends on whetherDerived
should be a template or whether it should inherit particular instance ofBase
:Template:
Non-template:
(where
Something
is some concrete type likeint
orchar *
orstd::string
)如果
您希望
Derived
能够与任何类型的Base<>
一起使用。或者,
如果您希望
Derived
仅适用于特定类型的Base
。Either
if you want
Derived
to work withBase<>
s of any type.Or
if you want
Derived
to only work with a specific type ofBase<>
.你的第二个例子就是要走的路。
Your second example is the way to go.
除了上述答案外,还请注意还可以通过以下方式:
或
Apart from the above answers, also be aware that it can be in following ways also:
or