简体   繁体   English

如何解决 sonarqube static 代码分析错误“显式定义缺少的复制构造函数,移动构造函数..”在 C++

[英]How to resolve the sonarqube static code analysis error “Explicitly define the missing copy constructor, move constructor ..” in C++

I am getting the below sonarqube static code analysis error:我收到以下 sonarqube static 代码分析错误:

Explicitly define the missing copy constructor, move constructor, copy assignment operator and move assignment operator so that they will not be implicitly provided.显式定义缺少的复制构造函数、移动构造函数、复制赋值运算符和移动赋值运算符,以便它们不会被隐式提供。

I am getting the above message at the below Destructor declaration in the header file:我在 header 文件中的以下 Destructor 声明中收到上述消息:

~CCPSDataManager();

And also in my.cpp file, there is definition for this Destructor ~CCPSDataManager().在 my.cpp 文件中,也有这个 Destructor ~CCPSDataManager() 的定义。

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 规则? Or is there any other approach?或者还有其他方法吗?

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.而且,如果我定义了复制构造函数、复制赋值运算符、移动构造函数和移动赋值运算符,我们将编写很多行代码而不使用它们。 Is this a correct approach?这是一个正确的方法吗?

Please suggest and give me some insight on how to proceed?请建议并给我一些关于如何进行的见解?

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 规则?

Depends on what you do in the destructor.取决于你在析构函数中做什么。

In most cases, if you need a destructor, then you need to follow rule of 5 because in most cases the implicitly generated ones do the wrong thing.在大多数情况下,如果您需要析构函数,那么您需要遵循 5 规则,因为在大多数情况下,隐式生成的析构函数会做错事。 Not always, but in most cases.并非总是如此,但在大多数情况下。 The analyser that you use suggests to follow the rule of 5 because of the assumption that it may be necessary.您使用的分析器建议遵循 5 规则,因为假设它可能是必要的。

Or is there any other approach?或者还有其他方法吗?

If there is no need for the custom destructor, then follow the rule of 0 instead: Don't define a custom destructor.如果不需要自定义析构函数,则遵循 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.而且,如果我定义了复制构造函数、复制赋值运算符、移动构造函数和移动赋值运算符,我们将编写很多行代码而不使用它们。

If you don't use them, then simplest solution is to define them deleted.如果您不使用它们,那么最简单的解决方案是将它们定义为删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM