当前位置: 首页 > wzjs >正文

网站上传可以通过org是国外的网站吗

网站上传可以通过,org是国外的网站吗,仿站网站域名,推介网文章目录1. 先说结论2. copy and move 代码示例3. 用 move_resource 代替 swap4. 对异常安全性的说明5. 关于 the rule of five 的说明C.21: If you define or delete any copy, move, or destructor function, define or delete them all1. 先说结论 对于管理资源的 class &a…

文章目录

  • 1. 先说结论
  • 2. copy and move 代码示例
  • 3. 用 move_resource 代替 swap
  • 4. 对异常安全性的说明
  • 5. 关于 the rule of five 的说明
    • C.21: If you define or =delete any copy, move, or destructor function, define or =delete them all


1. 先说结论

对于管理资源的 class ,使用 copy and swap 惯用法主要有 3 个好处:

  1. 避免在拷贝赋值运算符中,因内存不足抛出异常,破坏当前的对象。即具有异常安全性。
  2. 精简代码,将 2 个赋值运算符函数合二为一。
  3. 在赋值运算符函数中,省去了检查自赋值的操作。

我在 copy and swap 惯用法基础上做了一点改进,得到 copy and move ,既保留了上面的 3 个好处,还能够提升一点代码的运行效率。


2. copy and move 代码示例

示例代码如下,主要包括 5 个步骤,使用时直接套用这 5 个步骤即可:

  1. 创建默认构造函数。
  2. 在拷贝构造函数中实现 value semantics 值语义,即对 class 所有的资源数据,都创建一份独立的拷贝。
  3. 创建 move_resource 用于赋值运算符函数。 当成员变量的移动较复杂时,也可以用于移动构造函数。
  4. 在移动构造函数中转移资源。
  5. 创建统一的赋值运算符函数,将拷贝赋值运算符和移动复制运算符合二为一。
#include <memory>class CopyAndMove {public:CopyAndMove() = default;  // 1. 必须有默认构造函数。// 2. 在拷贝构造函数中实现值语义 value semantics,即对 class 所有的资源数据,都创建一份独立的拷贝。CopyAndMove(const CopyAndMove& other)  {if (other.data_ptr_) {  // 检查 nullptr*data_ptr_ = *other.data_ptr_;  // 使用解引用,拷贝数据。} };// 3. 创建 move_resource 用于赋值运算符函数。 当成员变量的移动较复杂时,也可以用于移动构造函数。void move_resource(CopyAndMove&& other) {// 直接把 other 中的数据,转移到当前对象 this 。this->data_ptr_ = std::move(other.data_ptr_);}// 4. 在移动构造函数中转移资源。CopyAndMove(CopyAndMove&& other) noexcept :  // 应明确标记 noexcept 。 data_ptr_{std::move(other.data_ptr_)} {// 如果涉及到较多较复杂的移动操作,也可能要调用 move_resource。// move_resource(std::move(other));  };// 5. 创建统一的赋值运算符函数,将拷贝赋值运算符和移动复制运算符合二为一。// 因为是以值传递的方式创建了 other ,所以在调用赋值运算符时,如果输入为左值,则会用拷贝构造函数// 创建 other。如果输入为右值,则会使用移动构造函数创建 other。CopyAndMove& operator=(CopyAndMove other) {// 创建 other 之后,可以直接调用 move_resource 转移其资源,无须进行自赋值验证。move_resource(std::move(other));return *this;}~CopyAndMove() = default;  // 应用 the rule of five ,明确定义 5 个函数。private:// data_ptr_ 创建时即初始化,实施 RAII 原则。 std::unique_ptr<int> data_ptr_{std::make_unique<int>(888)};
};

3. 用 move_resource 代替 swap

如果使用 copy and swap 惯用法,它的赋值运算符函数可能为如下形式。

CopyAndSwap& operator=(CopyAndSwap other) {// 其实并不需要把 *this 的数据交换给 other,因为 other 没有其它作用。swap(*this, other);  return *this;  }

在上面的函数中,将当前对象 *this 和 other 的数据进行交换。但实际上把 *this 的数据给 other 是一步多余的操作,因为 other 没有其它作用。如果把这一步骤省去,能够提升一点效率,因此我把 swap 替换成了 move_resource 函数,直接转移 other 的资源即可。


4. 对异常安全性的说明

上面提到 copy and swap 的第一个好处是异常安全性。这是相对于传统的拷贝赋值运算符来说。一个示例代码如下。

class MyArray {public:// 传统的拷贝赋值运算符函数(不使用 copy-and-swap)MyArray& operator=(const MyArray& other) {if (this != &other) {  // 自赋值检查。delete[] data_;  // 先释放当前资源,即当前对象 *this 的数据已丢弃,无法恢复。size_ = other.size_;// 下面开辟新的内存空间,可能因为内存不足抛出 bad_alloc,无法复制 other 。data_ = new int[size_];  std::copy(other.data_, other.data_ + size_, data_);}return *this;}int* data_;int size_;// 下面省略了其它代码。
};

在传统的拷贝赋值运算符中,因为会先释放当前资源,如果后续发生内存不足抛出异常,就等于破坏了当前对象,并且无法恢复。
而 copy and move 因为操作顺序相反,是先用拷贝构造函数生成 other ,再转移 other 的资源。所以即使生成 other 时因为内存不足抛出异常,当前对象也不会被破坏。


5. 关于 the rule of five 的说明

上面的析构函数写成 ~CopyAndMove() = default; 是应用了 C++ Core Guidelines 中的 the rule of five 原则。
The rule of five 的意思是:把 class 中的 5 个函数捆绑使用。即如果明确定义了其中的一个,则应该把另外四个也进行明确地定义,包括使用 = default 或 = delete 的方式。
这 5 个函数是:拷贝构造函数,移动构造函数,拷贝构造运算符,移动构造运算符,析构函数。
原文如下 : https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-five

C.21: If you define or =delete any copy, move, or destructor function, define or =delete them all

Reason The semantics of copy, move, and destruction are closely related, so if one needs to be declared, the odds are that others need consideration too.


—————————— 本文结束 ——————————


文章转载自:

http://WwO2qo5B.xfjwm.cn
http://1fhR9muT.xfjwm.cn
http://pKy6voNi.xfjwm.cn
http://3QcVLOlu.xfjwm.cn
http://ocgKCvMM.xfjwm.cn
http://zC91WGCb.xfjwm.cn
http://JeCXuzuI.xfjwm.cn
http://ckAwCCgh.xfjwm.cn
http://PjwuAGPA.xfjwm.cn
http://i5YdW6SH.xfjwm.cn
http://GI6u3u8V.xfjwm.cn
http://7tZ57pjT.xfjwm.cn
http://jD943W9y.xfjwm.cn
http://AVsdWVvb.xfjwm.cn
http://M6lLZkrl.xfjwm.cn
http://yPfy1UJO.xfjwm.cn
http://1WJggpIZ.xfjwm.cn
http://xUYr6W42.xfjwm.cn
http://udEKDTmJ.xfjwm.cn
http://oAcjXJie.xfjwm.cn
http://ZuB3vuSd.xfjwm.cn
http://gaNO9Ikk.xfjwm.cn
http://49V0W6r3.xfjwm.cn
http://AUnVD7MQ.xfjwm.cn
http://2t6LyaCj.xfjwm.cn
http://QZvQzBw6.xfjwm.cn
http://FoPyuIdj.xfjwm.cn
http://LgYG2okW.xfjwm.cn
http://HouNVYqv.xfjwm.cn
http://TB7TKdII.xfjwm.cn
http://www.dtcms.com/wzjs/622439.html

相关文章:

  • 网站备案他可以经营吗wordpress 瀑布流 插件
  • 网站平台建设专家意见学做网站格式工厂
  • 网站建设 jz.woonl外贸网站怎样注册
  • 公司网站开发多少钱公司网站建设优帮云
  • 邢台企业网站制作公司个人如何做微信下单小程序
  • 购物网站怎么做代码新手学做网站书
  • 信息网站建设预算jquery 显示 wordpress
  • 网站描述代码怎么写ps做网站首页的尺寸
  • 网站建设流程教程58同城二手房
  • 音乐网站页面设计wordpress增加文章页面
  • wap网站界面大连网站建设设计公司
  • 大型网站是用哪种数据库做的wordpress 给标签加id
  • 网站开发网站制作三亚市建设局官方网站
  • 岱岳区网站设计电脑网页翻译
  • 如何把html网站改为asp网站网站开发后期要解决的问题
  • phpnow 搭建网站网络安全行业公司排名
  • 网站报错40121天网站建设实录
  • 怎么使用网站模板建设商业网站
  • 网站代码加密用ip地址做网站
  • 成都医院手机网站建设设计公司的企业使命
  • 云莱坞网站开发品牌建设的十六个步骤
  • 怎么做网站打赏wordpress手机文章
  • 常州如何进行网站推广增城线上教学
  • 网站开发遇到的问题及解决方法肇庆网站制作软件
  • 江苏网站建设系统服务中山 家居 骏域网站建设专家
  • 想做个自己的网站登录app
  • 湟中网站建设深圳网站制作公司售后服务
  • 陈仓网站建设在线做ppt
  • 杭州网站设计 site网络营销案例分享
  • 红酒 网站 模板西安网站建设那家强