☰
  • 首页
  • 规则分类
  • 项目介绍
search
•••

存在拷贝构造函数或拷贝赋值运算符时,不应缺少析构函数

5.1.8 ID_missingDestructor
目录 › next › previous

三个紧密相关的函数:

  1. 拷贝构造函数
  2. 拷贝赋值运算符
  3. 析构函数

当这三个函数中的任何一个函数被定义时,其他两个函数也需要被定义,详见“Rule of three”。

示例:

class A {  // Non-compliant, missing destructor
public:
    A();
    A(const A&);
    A& operator = (const A&);
};

应明确定义析构函数:

class A {  // Compliant
public:
    A();
    A(const A&);
    A& operator = (const A&);

   ~A();   // Destructor
};

相关

ID_missingCopyConstructor ID_missingCopyAssignOperator ID_violateRuleOfFive

参考

C++ Core Guidelines C.21 C++ Core Guidelines C.30 C++ Core Guidelines C.33
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.