【问题标题】:How do you inherit from template class when you need to inherit not from template?当您需要不从模板继承时,如何从模板类继承?
【发布时间】:2011-01-31 05:13:22
【问题描述】:

听起来很糟糕……但是有

template < int ArrayLength, typename SomeValueType > class SomeClass{
    SomeValueType SomeValue;
    SomeValueType SomeArray[ ArrayLength ];
    ...
};

例如,你如何 ceae 扩展 SomeClass 的类,即 SomeClass &lt; 20, int &gt;

有点像

class MyClass : SomeClass &lt; 20, int &gt; {...};正确方法?

【问题讨论】:

  • 你知道这是默认的私有继承吗?使用类 MaClass : public SomeClass{...};拥有公共继承权。
  • 如果MyClass 可以通过SomeClass* 删除,那么您还应该向SomeClass 添加一个虚拟析构函数,以确保执行任何其他MyClass 特定的销毁步骤......跨度>

标签: c++ oop templates inheritance


【解决方案1】:

如果你想要私有继承:

class MyClass : SomeClass < 20, int > //private by default!
{ 
    //...
};

如果你想要公共继承:

class MyClass : public SomeClass < 20, int > 
{ 
    //...
};

【讨论】:

    猜你喜欢
    • 2018-01-18
    • 2018-05-23
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    相关资源
    最近更新 更多