c++ copy and swap以及三法则
实现operator=时的常用技巧
class Point{
private:
int x;
int y;
public:
Point(const Point& p):x{p.x},
y{p.y}{}
};
class Rectangle{
private:
Point *point;
public:
Rectangle(){
point = nullptr;
}
~Rectangle(){delete point;}
Rectangle(const Rectangle& rect):point(rect.point ? new Point{*rect.point} : nullptr){}
Rectangle& operator=(const Rectangle& rect){
if(this == &rect){
return *this;
}
Rectangle tmp{rect}; // 使用拷贝构造函数进行copy
std::swap(point, tmp.point); // 使用swap进行替换
}
};
使用一个临时变量进行copy然后swap是一个很巧妙的方式,否则就需要大量的判断this->point是否为nullptr, rect.point是否为nullptr做很多处理,但这里最关键的是拷贝构造函数Rectangle(const Rectangle& rect)
要写对;
这里引入了李建忠老师说的三法则,如果定义了析构函数,那是拷贝构造函数和赋值函数都是需要定义的,因为析构一定涉及到非字面字节拷贝的资源的处理,存在这样的资源的时候,使用编译器生成的拷贝构造函数和赋值函数是会有问题的,比如上面的Rectangle->point; 但如果能保证一定不涉及到拷贝赋值操作,那在规范上最好delete处理;
class Rectangle{
private:
Point *point;
public:
Rectangle(){
point = nullptr;
}
~Rectangle(){delete point;}
Rectangle(const Rectangle& rect) = delete; //不使用
Rectangle& operator=(const Rectangle& rect) = delete; //不使用
};
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2020-05-04 andorid(3) 使用sqllite进行数据持久化
2020-05-04 android(1)--hello world中的layout与 onCreate()
2020-05-04 android(2)--listView