模板类赋值操作符类

Template class assignment operator class

本文关键字:赋值操作符      更新时间:2023-10-16

我有一个TemplateArray和CharArray类。

我如何使templatearray的赋值操作符仅从chararray类复制时,templatearray是相同类型(即char)或类似类型(即unsigned char)的chararray?

TemplateArray和CharArray在功能上是相同的(除了CharArray可以处理以NULL结尾的字符串)。

例如:

template<typename TemplateItem>
TemplateList & TemplateList<TemplateItem>::operator=(const CharArray &ItemCopy)
{
    //How do I only copy when TemplateList is of type char (or similar unsigned char)
    //IE is same/similar to CharArray
    //Both classes are functionally the same, except CharArray is chars only
}

看起来您需要TemplateList::operator=的专门化:

template<>
TemplateList& TemplateList<char>::operator=(const CharArray &ItemCopy)
{
    // Do the copying here, you don't provide enough
    // information for a practical suggestion
}