如何解决C++中的sonarqube静态代码分析错误"Explicitly define the missing copy constructor, move constructor .."

How to resolve the sonarqube static code analysis error "Explicitly define the missing copy constructor, move constructor .." in C++

我收到以下 sonarqube 静态代码分析错误:

显式定义缺失的复制构造函数、移动构造函数、复制赋值运算符和移动赋值运算符,使其不会被隐式提供。

我在头文件中的以下析构函数声明中收到上述消息:

~CCPSDataManager();

而且在我的 .cpp 文件中,有这个析构函数的定义 ~CCPSDataManager()。

这里是否需要遵循规则5提供析构函数、复制构造函数和复制赋值运算符、移动构造函数和移动赋值运算符?或者还有其他方法吗?

而且,如果我定义复制构造函数、复制赋值运算符、移动构造函数和移动赋值运算符,我们将在不使用它们的情况下编写很多代码行。这是正确的做法吗?

请提出建议并让我了解如何进行?

Here do I need to follow the rule of 5 by providing the Destructor, copy constructor and the copy-assignment operator, move constructor and the move-assignment operator?

取决于您在析构函数中执行的操作。

在大多数情况下,如果您需要析构函数,则需要遵循规则 5,因为在大多数情况下,隐式生成的析构函数会做错事。并非总是如此,但在大多数情况下。您使用的分析器建议遵循 5 的规则,因为假设它可能是必要的。

Or is there any other approach?

如果不需要自定义析构函数,则遵循规则 0:不要定义自定义析构函数。

And also If I define the copy constructor, copy-assignment operator, move constructor and the move-assignment operator we are writing many lines of code without using those.

如果您不使用它们,那么最简单的解决方案是将它们定义为删除。