返回*this
和this
与通过引用返回对象的模板函数有什么区别? 这两个选项在VS2013中没有任何问题编译;返回参考模板
的代码是一样的东西
template <typename T>
class MyClass
{
public:
MyClass(){ }
~MyClass();
MyClass& operator=(const MyClass&);
};
template <typename T>
MyClass<T>& MyClass<T>::operator=(const MyClass& s_from)
{
//do some work
return *this;
//also works
//return this
}
template <typename T>
MyClass<T>::~MyClass()
{
}
'retun this;'不应该编译。如果有,请制作[mcve]。 – nwp
它编译。这就是为什么我问。 –
当你实例化你的模板并使用operator =时,你会得到错误。看到我的回答 – AnatolyS