Rule of Three in C++

If you know that the copy constructor won’t be used, you can express that by making it private and unimplemented, thus: class C { private: C(const C&); // not implemented }; (in C++11 you can use the new = delete syntax). That said, you should only do that if you’re absolutely sure it will never … Read more

Rule-of-Three becomes Rule-of-Five with C++11? [closed]

I’d say the Rule of Three becomes the Rule of Three, Four and Five: Each class should explicitly define exactly one of the following set of special member functions: None Destructor, copy constructor, copy assignment operator In addition, each class that explicitly defines a destructor may explicitly define a move constructor and/or a move assignment … Read more

What is The Rule of Three?

Introduction C++ treats variables of user-defined types with value semantics. This means that objects are implicitly copied in various contexts, and we should understand what “copying an object” actually means. Let us consider a simple example: class person { std::string name; int age; public: person(const std::string& name, int age) : name(name), age(age) { } }; … Read more