Understanding the Rule of Zero

I have a base class and I don't want the derived class to be copied. To make everything explicit, I implement it this way:

class A { public: A() = default; virtual ~A() = default; A(const A&) = delete; A(const A&&) = delete; A& operator=(const A&) = delete; A& operator=(const A&&) = delete; virtual void vFun() = 0; }; class B : public A { public: B() = default; virtual ~B() = default; B(const B&) = delete; B(const B&&) = delete; B& operator=(const B&) = delete; B& operator=(const B&&) = delete; virtual void vFun() override {} }; 

Is this the right way to do such things? According to my knowledge and what I read, the answer is yes, but I would like to be sure before introducing it into the production system.


EDIT

Introducing conclusions: 1) Almost always move operators should not be deleted. This is because "there is an infinity of things that require mobility." 2) For an abstract base class, it is safer to allow the compiler to generate a special member function and move the delete to the derived class if such a need exists.

+5
source share
1 answer

No, this is completely untrue.

First, in your quest to make the derived class non-copyable, you made it non-movable, which makes it almost useless.

Secondly, there is no reason why A should not be copied at all. Each derived class can simply make itself non-copyable if it wants. A is already abstract and cannot be sliced, so there is no reason to make A noncopyable.

+5
source

All Articles