C++库研究笔记——赋值操作符operator=的正确重载方式(三个准则)
总结了下,更加标准的写法是:
template <typename T>
array1d<T>& array1d<T>::operator=(const array1d<T>& other)
{
if(this!= &other)
{
if((*this).size()!=other.size())
{
deallocate();
size_= other.size();
allocate();
}
for(int i=0; i<size_; i++){
data_[i]=other[i];
}
}
return *this;
}