Copy/Move operations for new classes

Hi

Should a new class always be provided a copy constructor, copy assignment, move constructor and move assignment?

Is this true for _all_ of the following types of classes:
1 Concrete classes
2 Classes derived from concrete classes
3 Classes derived from abstract classes
4 Class templates

Thanks
Concrete class: Rule of Zero:
Classes that have custom destructors, copy/move constructors or copy/move assignment operators should deal exclusively with ownership (which follows from the Single Responsibility Principle). Other classes should not have custom destructors, copy/move constructors or copy/move assignment operators.

1
2
3
4
5
6
class rule_of_zero
{
    std::string cppstring;
 public:
    rule_of_zero(const std::string& arg) : cppstring(arg) {}
};


Abstract base class / class designed for run-time polymorphism: Rule of Five Defaults
When a base class is intended for polymorphic use, its destructor may have to be declared public and virtual. This blocks implicit moves (and deprecates implicit copies), and so the special member functions have to be declared as defaulted

1
2
3
4
5
6
7
8
9
class base_of_five_defaults
{
 public:
    base_of_five_defaults(const base_of_five_defaults&) = default;
    base_of_five_defaults(base_of_five_defaults&&) = default;
    base_of_five_defaults& operator=(const base_of_five_defaults&) = default;
    base_of_five_defaults& operator=(base_of_five_defaults&&) = default;
    virtual ~base_of_five_defaults() = default;
};

http://en.cppreference.com/w/cpp/language/rule_of_three

For derived classes etc, the general rule is: Favour rule of zero if you can, but
if you declare one of these five function, you explicitly declare all.

If you declare any of those you must consider all and explicitly define or default the ones you want. Think of copying, moving, and destruction as closely related operations, rather than individual operations that you can freely mix and match - you can specify arbitrary combinations, but only a few combinations make sense semantically.
http://www.stroustrup.com/C++11FAQ.html#default2
Topic archived. No new replies allowed.