Template class assignment operator class

空扰寡人 提交于 2019-12-13 03:11:28

问题


I have a TemplateArray and a CharArray class.

How do I make it so the templatearray's assignment operator only copies in from the chararray class when the templatearray is of the same type (I.E. char) or similar type (I.E. unsigned char) to chararray?

TemplateArray and CharArray are functionally the same (except CharArray can handle NULL terminated strings).

For example:

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
}

回答1:


It looks like you need a specialization of TemplateList::operator=:

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


来源:https://stackoverflow.com/questions/7377585/template-class-assignment-operator-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!