我正在研究五法则及其近亲(四法则和 1/2、复制和交换习语、朋友交换函数)。
我在测试课上实施了四和 1/2 规则。它编译得很好。我的实现中是否存在任何隐藏的错误?
我特别关注存储在m_unoreredd_map属性中的 unique_ptrs,我将其移动到复制构造函数中,因为它们无法复制。这是处理类中 unique_ptr 的正确方法吗?
#ifndef SOMECLASS_H
#define SOMECLASS_H
#include "someotherclass.h"
#include <QString>
#include <QStringList>
#include <memory>
#include <string>
#include <unordered_map>
class SomeClass
{
QString m_qstring;
std::unordered_map<std::string, std::unique_ptr<SomeOtherClass>> m_unordered_map;
int m_int;
std::string m_string;
QStringList m_qstringlist;
public:
SomeClass() = default;
// Rule of 5 (or Rule of 4 and 1/2)
// From https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom#3279550
~SomeClass() = default; // Destructor
SomeClass(SomeClass &other); // Copy constructor
SomeClass(SomeClass &&other); // Move constructor
SomeClass &operator=(SomeClass other); // Copy/Move assignment …Run Code Online (Sandbox Code Playgroud)