C++库研究笔记——赋值操作符operator=的正确重载方式(2)

来源:互联网 发布:软件功能模块图 编辑:程序博客网 时间:2024/05/16 17:12

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;}



原创粉丝点击