The “Rule-of-Zero“ should be followed (s4963)

Most classes should not directly handle resources, but instead, use members that perform resource handling for them:

  • For memory, it can be std::unique_ptrstd::shared_ptrstd::vector…​
  • For files, it can be std::ofstreamstd::ifstream…​
  • …​

Classes that avoid directly handling resources don’t need to define any of the special member functions required to properly handle resources: destructor, copy constructor, move constructor, copy-assignment operator, move-assignment operator. That’s because the versions of those functions provided by the compiler do the right thing automatically, which is especially useful because writing these functions correctly is typically tricky and error-prone.

Omitting all of these functions from a class is known as the Rule of Zero because no special function should be defined.

In some cases, this rule takes a slightly different shape, while respecting the fact that no definition of those functions will be provided:

  • For the base class of a polymorphic hierarchy, the destructor should be declared as public and virtual, and defaulted (=default). The copy-constructor and copy-assignment operator should be deleted. (If you want to copy classes in a polymorphic hierarchy, use the clone idiom.) The move operation will be automatically deleted by the compiler.
  • For other kinds of base classes, the destructor should be protected and non-virtual, and defaulted (=default).

Noncompliant Code Example

class FooPointer { // Noncompliant. The code is correct (it follows the rule of 5), but unnecessarily complexFoo* pFoo;
public:FooPointer(int initValue) {pFoo = new Foo(initValue);}~FooPointer() {delete pFoo;}FooPointer(FooPointer const &fp) = delete;FooPointer const & operator=(FooPointer const &fp) = delete;FooPointer(FooPointer &&fp) noexcept {pFoo = fp.pFoo;fp.pFoo = nullptr;}FooPointer const & operator=(FooPointer &&fp) {FooPointer temp(std::move(fp));std::swap(temp.pFoo, pFoo);return *this;}
};

Compliant Solution

class FooPointer { // Compliant, std::unique_ptr is use to handle memory managementunique_ptr<Foo> pFoo;
public:FooPointer(int initValue) : pFoo(std::make_unique<Foo>(initValue) {}
};

A polymorphic base class can look like this:

class Base { // Compliant, the virtual destructor is defaulted
public:virtual ~Base() = default;Base(Base const &) = delete;Base &operator=(Base const &) = delete;
};

Exceptions

  • Empty destructors are treated as though they were defaulted.
  • There are several cases when this rule should not be followed. For instance, if your class is manually handling a resource, logging when being constructed/copied, maintaining some kind of counter, having non-transient data that should not be copied (like capacity for std::vector)…​ In that case, it should still follow the rule of 5 (S3624). And you should consider if you can isolate this specific behavior in a base class or a dedicated member data, which would allow you to still follow the rule of 0.

See

  • S3624
  • S1235

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/71181.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

在Ubuntu 22.04 LTS 上安装 MySQL两种方式:在线方式和离线方式

Ubuntu安装MySQL 介绍&#xff1a; Ubuntu 是一款基于Linux操作系统的免费开源发行版&#xff0c;广受欢迎。它以稳定性、安全性和用户友好性而闻名&#xff0c;适用于桌面和服务器环境。Ubuntu提供了大量的软件包和应用程序&#xff0c;拥有庞大的社区支持和活跃的开发者社区…

用Java编写sql

1.概念 通过Java代码操作mysql数据库 数据库编程&#xff0c;是需要数据库服务器&#xff0c;提供一些API&#xff0c;供程序员调用的 2.安装 2.1下载 在程序中操作mysql需要先安装mysql的驱动包 并且要把驱动包引入到项目中 在中央仓库可以下载到驱动包(mvnrepository.…

在Ubuntu中,某个文件的右下角有一把锁的标志是什么意思?

在Ubuntu中&#xff0c;某个文件的右下角有一把锁的标志是什么意思&#xff1f; 在 Ubuntu&#xff08;或其他基于 GNOME 文件管理器的 Linux 发行版&#xff09;中&#xff0c;文件或文件夹的右下角出现一把“锁”标志&#xff0c;通常表示 你当前的用户没有该文件/文件夹的写…

Redis数据结构-List列表

1.List列表 列表类型适用于存储多个有序的字符串&#xff08;这里的有序指的是强调数据排列顺序的重要&#xff0c;不是升序降序的意思&#xff09;&#xff0c;列表中的每个字符串称为元素&#xff08;element&#xff09;&#xff0c;一个列表最多可以存储2^32-1个元素。在R…

《论负载均衡技术在Web系统中的应用》审题技巧 - 系统架构设计师

软考论文写作框架 一、考点概述 本题考点主要围绕“负载均衡技术在Web系统中的应用”展开,旨在考察考生对负载均衡技术的理解、应用及项目管理经验。负载均衡技术是提升Web系统性能的关键手段,通过合理分配和分散系统负载,确保多个操作单元能够高效协同工作,从而提升系统…

Linux实操——在服务器上直接从百度网盘下载(/上传)文件

Linux Linux实操——在服务器上直接从百度网盘下载&#xff08;/上传&#xff09;文件 文章目录 Linux前言一、下载并安装bypy工具二、认证并授权网盘账号三、将所需文件转移至目的文件夹下四、下载文件五、上传文件六、更换绑定的百度云盘账户 前言 最近收到一批很大的数据&…

报错The default superclass, “jakarta.servlet.http.HttpServlet“(已经配置好tomcat)

报错报错DescriptionResourcePathLocationType The default superclass,“jakarta.servlet.http.HttpServlet”, according to the project’s Dynamic Web Module facet version (5.0), was not found on the Java Build Path. 解决办法&#xff1a; 根据错误信息&#xff0…

【UI设计——陕西红富士苹果海报分享】

陕西红富士苹果海报设计分享 为大家带来一款陕西红富士苹果的宣传海报设计。 海报以柔和的粉色为背景&#xff0c;营造出温馨的氛围。画面下方展示了色泽红润、形态饱满的红富士苹果&#xff0c;既有完整的果实&#xff0c;也有切开的剖面&#xff0c;直观呈现其诱人外观。 上…

题解 | 牛客周赛82 Java ABCDEF

目录 题目地址 做题情况 A 题 B 题 C 题 D 题 E 题 F 题 牛客竞赛主页 题目地址 牛客竞赛_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ 做题情况 A 题 判断字符串第一个字符和第三个字符是否相等 import java.io.*; import java.math.*; import java.u…

vulkanscenegraph显示倾斜模型(5)-视景器准备

前言 本文在接着往下讨论视景器准备相关步骤。Vulkan相比opengl更底层,其提供了更底层的硬件控制、更高的性能以及更好的多线程支持,VSG 通过封装 Vulkan 的复杂性,提供了更简单易用的接口,同时保留了 Vulkan 的高性能和灵活性。它简化了 Vulkan 的初始化、渲染管线配置、资…

基金 word-->pdf图片模糊的解决方法

1. 首先需要Adobe或福昕等pdf阅读器。 2. word中 [文件]--[打印]&#xff0c;其中打印机选择pdf阅读器&#xff0c;例如此处我选择福昕阅读器。 3. 选择 [打印机属性]--[编辑]--[图像]&#xff0c;将所有的采样、压缩均设置为 关闭。点击[另存为]&#xff0c;保存为 基金报告…

基于RKNN的嵌入式深度学习开发(2)

上一个章节我们介绍的RKNN模型的模型转换和模型的推理&#xff0c;这一章节我们将介绍模型的量化和评估部分。 2.3 RKNN模型的量化 量化就是将浮点转换为定点运算的过程&#xff0c;或者训练后由rknn来量化。量化模型使用较低精度&#xff08;如int8/uint8/int16&#xff09;保…

单一职责原则(设计模式)

目录 问题&#xff1a; 定义&#xff1a; 解决&#xff1a; 方式 1&#xff1a;使用策略模式 示例&#xff1a;用户管理 方式 2&#xff1a;使用装饰者模式 示例&#xff1a;用户操作 方式 3&#xff1a;使用责任链模式 示例&#xff1a;用户操作链 总结 推荐 问题&a…

Java 8 到 Java 17 主要新特性

Java 8 到 Java 17 是 Java 语言的多个重要版本&#xff0c;其中每个版本都引入了新的特性和改进。下面是 Java 8 到 Java 17 中主要的新特性概览。 Java 8 新特性 1. Lambda 表达式 Lambda 表达式是 Java 8 的一个重要特性&#xff0c;它使得 Java 支持函数式编程&#xff…

Qt 中signals和slots、Q_SIGNAL和Q_LOT、Q_SIGNALS和Q_SLOTS的区别和使用

Qt 中signals和slots、Q_SIGNAL和Q_SLOT、Q_SIGNALS和Q_SLOTS的区别和使用 1.signals和slots 信号和槽函数需要在类的声明中明确声明。信号需要使用signals关键字&#xff0c;而槽函数可以使用slots关键字&#xff08;虽然在现代Qt中&#xff0c;槽函数也可以直接作为普通成员…

【极客时间】浏览器工作原理与实践-2 宏观视角下的浏览器- 2.1 Chrome架构:仅仅打开了1个页面,为什么有4个进程?

https://time.geekbang.org/column/article/113513 2.1 Chrome架构&#xff1a;仅仅打开了1个页面&#xff0c;为什么有4个进程&#xff1f; 前置&#xff1a;基于Chrome浏览器学习浏览器的工作原理 原因&#xff1a; 因为 Chrome、微软的 Edge 以及国内的大部分主流浏览器…

智能图像处理平台:图像处理配置类

这里我们先修改一下依赖&#xff0c;不用JavaCV&#xff0c;用openCV。 导入依赖&#xff1a; <!-- JavaCV 依赖&#xff0c;用于图像和视频处理 --> <!-- <dependency>--> <!-- <groupId>org.bytedeco</groupId>--> &l…

【Python 初级函数详解】—— 参数沙漠与作用域丛林的求生指南

欢迎来到ZyyOvO的博客✨&#xff0c;一个关于探索技术的角落&#xff0c;记录学习的点滴&#x1f4d6;&#xff0c;分享实用的技巧&#x1f6e0;️&#xff0c;偶尔还有一些奇思妙想&#x1f4a1; 本文由ZyyOvO原创✍️&#xff0c;感谢支持❤️&#xff01;请尊重原创&#x1…

夜天之书 #106 Apache 软件基金会如何投票选举?

近期若干开源组织进行换届选举。在此期间&#xff0c;拥有投票权的成员往往会热烈讨论&#xff0c;提名新成员候选人和治理团队的候选人。虽然讨论是容易进行的&#xff0c;但是实际的投票流程和运作方式&#xff0c;在一个成员众多的组织中&#xff0c;可能会有不少成员并不清…

【蓝桥】大小写转换

1、islower()和isupper() 1.1 islower()函数 定义&#xff1a;用于判断一个字符是否为小写字母&#xff08;即 ‘a’ 到 ‘z’&#xff09; 1.2 isupper()函数 定义&#xff1a;用于判断一个字符是否为大写字母&#xff08;即 ‘A’ 到 ‘Z’&#xff09; 1.3 基础用法 #incl…