为了澄清,我引用了 James McNellis 在 the post "Template assignment operator overloading mystery" 中的回答:
The implicitly-declared copy assignment operator, which is declared as follows, is still generated:
Wrapper& operator=(const Wrapper&);
现在我有一个类似的类,想知道这个运算符的定义需要是什么样的。
这是召回类:
template<typename T>
struct Wrapper;
现在的校正匹配是什么:
template<typename T>
Wrapper& Wrapper<T>::operator=(const Wrapper&)
或
Wrapper& Wrapper::operator=(const Wrapper&)
?
还是这只是一样?
最佳答案
这个
template<typename T>
Wrapper& Wrapper<T>::operator=(const Wrapper&)
这实际上只是
的简写template<typename T>
Wrapper<T>& Wrapper<T>::operator=(const Wrapper<T>&)
另一个版本适用于名为 Wrapper
的非模板类,它对您的模板没有影响。
关于c++ - 模板类的赋值运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7867320/