template <typename T>
class store // Very basic class, capable of accepting any data-type and does nothing too much
{
public:
store(T value) : value(value) {}
private:
T value;
}
template <>
class store<int> // Inherits all the basic functionality that the above class has and it also has additional methods
: public store<int> // PROBLEM OVER HERE. How do I refer to the above class?
{
public:
store(int value) : store<int>(value) /* PROBLEM OVER HERE. Should refer to the constructor of the above class */ {}
void my_additional_int_method();
}
这里我有继承问题。我不想更改基类的名称,因为基类与所有派生类的用途相同(唯一的区别 - 派生类只有很少的额外方法)
答案 0 :(得分:2)
你可以这样做:
template <typename T>
class store_impl
{
public:
store_impl(T value) : value(value) {}
private:
T value;
}
// default class accepting any type
// provides the default methods
template <typename T>
class store: public store_impl<T>
{
public:
store(T value) : store_impl(value) {}
}
// specialization for int with extra methods
template <>
class store<int>: public store_impl<int>
{
public:
store(int value) : store_impl<int>(value)
{}
void my_additional_int_method();
}
答案 1 :(得分:0)
您无法为类提供专用模板的名称:
template <>
class store<int>
你能做的就是给它一个具体的类型名称:
class store_int : public store<int>
或使用typedef
或using
声明
typdef store<int> store_int;
using store_int = store<int>;