您的位置:首页 > 运维架构

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

2013-09-28 03:11 260 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: