Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 父类构造函数的模板专门化_C++_Templates_Inheritance_Specialization - Fatal编程技术网

C++ 父类构造函数的模板专门化

C++ 父类构造函数的模板专门化,c++,templates,inheritance,specialization,C++,Templates,Inheritance,Specialization,我得到了一个BaseType,它是模板化的,希望用一个ArrayItem类继承它。因为我想将它们用作内存的模板,所以我想让ArrayItem类知道我们使用的是哪种类型。所以我想专门化一些模板值的构造函数,例如long-long template<typename T> class ArrayItem : public BaseType<T> { public: inline ArrayItem(T& t);

我得到了一个
BaseType
,它是模板化的,希望用一个ArrayItem类继承它。因为我想将它们用作内存的模板,所以我想让ArrayItem类知道我们使用的是哪种类型。所以我想专门化一些模板值的构造函数,例如long-long

    template<typename T>
    class ArrayItem : public BaseType<T>
    {
    public:
        inline ArrayItem(T& t);
        inline ETypes getType();
    private:
        ETypes m_type;
    };
模板
类ArrayItem:公共基类型
{
公众:
内联阵列项目(T&T);
内联ETypes getType();
私人:
ETypes m_型;
};
水电站应该是这样的:

template <typename T>
ArrayItem<T>::ArrayItem (T& t): BaseType(t)
{
}

template <>
ArrayItem<long long>::ArrayItem(long long& t) : BaseType<long long>(t) // this
{
    m_type = INT;
}


template<typename T>
inline ETypes ArrayItem<T>::getType()
{
    return m_type;
}
模板
ArrayItem::ArrayItem(T&T):基类型(T)
{
}
模板
ArrayItem::ArrayItem(long-long&t):BaseType(t)//这个
{
m_type=INT;
}
模板
内联ETypes ArrayItem::getType()
{
返回m_类型;
}
但是我如何在这里做这个专门化呢


enum ETypes
{
INT,
布尔,
对象
阵列,
双重的
一串
};
模板
类基类
{
公众:
BaseType();
显式基类型(T&T);
受保护的:
联合数据联合
{
T数据;
size\u t size;//使其至少为64位
显式数据联合(T&T);
}m_数据;
};
模板
BaseType::DataUnion::DataUnion(T&T)
{
这个->数据=t;
}
模板
BaseType::BaseType(T&T):m_数据(T){}
模板
类ArrayItem:公共基类型
{
公众:
显式内联数组项(T&T);
内联ETypes getType();
私人:
ETypes m_型;
};
模板
ArrayItem::ArrayItem(T&T):基类型(T)
{
}
模板
ArrayItem::ArrayItem(long-long&t):BaseType(t)//这个
{
m_type=INT;
}
模板
内联ETypes ArrayItem::getType()
{
返回m_类型;
}
int main()
{
长somenumber=1234;
ArrayItem项(somenumber);
if(item.getType()==INT)

std::cout在我看来,除了没有为典型情况下的
BaseType
提供模板参数之外,您所拥有的一切都是正确的

下面是一个简单的演示:

#include <iostream>

template <typename T>
struct B { };

template <typename T>
struct D : B<T> {
    D(T );
};

template <typename T>
D<T>::D(T )
    : B<T>()
{
    std::cout << "def\n";
}

template <>
D<long>::D(long ) 
    : B<long>()
{
    std::cout << "hi\n";
}

int main()
{
    D<int> i(4);  // prints def
    D<long> l(5); // prints hi
}
#包括
模板
结构B{};
模板
结构D:B{
D(T);
};
模板
D::D(T)
:B()
{

std::cout您当前的方法有什么不起作用?我不能编译它,但我想长期专门化该类,以便构造函数将m_类型设置为value INT。(这是一个简单的枚举)具体的编译错误是什么?
ArrayItem不是声明行的模板:
ArrayItem::…
您能提供一个模板吗?
#include <iostream>

template <typename T>
struct B { };

template <typename T>
struct D : B<T> {
    D(T );
};

template <typename T>
D<T>::D(T )
    : B<T>()
{
    std::cout << "def\n";
}

template <>
D<long>::D(long ) 
    : B<long>()
{
    std::cout << "hi\n";
}

int main()
{
    D<int> i(4);  // prints def
    D<long> l(5); // prints hi
}