【问题标题】:Template class assignment operator class模板类赋值运算符类
【发布时间】:2011-11-14 17:01:31
【问题描述】:

我有一个 TemplateArray 和一个 CharArray 类。

当模板数组与 chararray 具有相同类型(即 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
}

【问题讨论】:

  • 一个代码示例值 1000 字 ;)
  • 好的。很多头文件我不能复制粘贴,但我会引用函数行。
  • 我认为你只能通过模板专门化“char”来做到这一点。否则你将不得不通过实现反射或其他某种机制来找到运行时类型识别的方法..

标签: c++ templates assignment-operator


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2019-06-30
    • 2015-01-27
    • 2016-01-04
    • 2016-05-28
    • 2011-05-23
    • 2016-09-03
    • 2011-05-29
    相关资源
    最近更新 更多