如何继承模板化的基类?

发布于 2024-11-01 21:11:47 字数 637 浏览 1 评论 0原文


我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风筝在阴天搁浅。 2024-11-08 21:11:47

后者

class Derived : public Base

不起作用,因为Base不是一个类,它是一个类模板。这取决于 Derived 是否应该是一个模板,或者它是否应该继承 Base 的特定实例:

  • Template:

    模板<类型名称 T>
    派生类:public Base
    
  • 非模板:

    派生类:public Base;
    

    (其中 Something 是一些具体类型,例如 intchar *std::string)< /p>

The later,

class Derived : public Base

will not work, because Base is not a class, it's a class template. Than it depends on whether Derived should be a template or whether it should inherit particular instance of Base:

  • Template:

    template<typename T>
    class Derived : public Base<T>
    
  • Non-template:

    class Derived : public Base<Something>
    

    (where Something is some concrete type like int or char * or std::string)

星光不落少年眉 2024-11-08 21:11:47

如果

template class<T>  
class Derived : public Base<T>  
{ };

您希望 Derived 能够与任何类型的 Base<> 一起使用。

或者,

class Derived : public Base<SomeSpecificType>
{ };

如果您希望 Derived 仅适用于特定类型的 Base

Either

template class<T>  
class Derived : public Base<T>  
{ };

if you want Derived to work with Base<>s of any type.

Or

class Derived : public Base<SomeSpecificType>
{ };

if you want Derived to only work with a specific type of Base<>.

安静 2024-11-08 21:11:47

你的第二个例子就是要走的路。

template class<T>   
public Base<T>   
{    // Implementation of Base constructor etc and methods ...   
}; 


template class<T>   
class Derived : public Base<T>   
{    // Implementation of Derived constructor etc and methods ...   
public:
    Derived() : Base<T>() { } //have to construct the proper instanciation of Base

}; 

Your second example is the way to go.

template class<T>   
public Base<T>   
{    // Implementation of Base constructor etc and methods ...   
}; 


template class<T>   
class Derived : public Base<T>   
{    // Implementation of Derived constructor etc and methods ...   
public:
    Derived() : Base<T>() { } //have to construct the proper instanciation of Base

}; 
不如归去 2024-11-08 21:11:47

除了上述答案外,还请注意还可以通过以下方式:

template<typename T>
class Derived : public Base<Something>  // Something is some concrete type

template<typename T, typename U>
class Derived : public Base<U>

Apart from the above answers, also be aware that it can be in following ways also:

template<typename T>
class Derived : public Base<Something>  // Something is some concrete type

or

template<typename T, typename U>
class Derived : public Base<U>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文