Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding rule of zero

I have a base class, and I do not want to make derived class copyable. In order to make everything explicit I implement it in that 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 correct way of doing such things? According to my knowledge and what I have read, the answer is yes, but I would like to be sure before I introduce it into production system.


EDIT

Taking things into conclusion: 1) Almost always move operators should not be deleted. That's because "there's an infinity of things that require movability". 2) For abstract base class, it's safer to allow compiler to generate special member function, and move deletion into derived class if such necessity exists.

like image 874
galvanize Avatar asked Sep 13 '15 10:09

galvanize


People also ask

What is the rule for zero?

An alternative to “The Rule of The Big Four (and a half)” has appeared in the form of “The Rule of Zero”. “The Rule of Zero” basically states: You should NEVER implement a destructor, copy constructor, move constructor or assignment operators in your code.

How does the zero power rule work?

Therefore, it is proven that any number or expression raised to the power of zero is always equal to 1. In other words, if the exponent is zero then the result is 1. The general form of zero exponent rule is given by: a 0 = 1 and (a/b) 0 = 1. 0° = undefined.

What does 2 to the power of 0 mean?

So 2 to the zero power is going to be equal to 1. And, actually, any non-zero number to the 0 power is 1 by that same rationale.

What does 4 to the power of 0 mean?

Answer: 4 to the power of 0 is 1. According to the zero property of exponents, any number (other than 0) raised to the power of zero is always equal to 1. So, 4 to the power of 0 can be written as 40 which is equal to 1.


1 Answers

Nope, this is completely incorrect.

Firstly, in your quest to make the derived class noncopyable, you have made it non-movable, which renders it nearly useless.

Secondly, there's no reason for A to be noncopyable at all. Each derived class can just make itself noncopyable if it wants to. A is already abstract and cannot be sliced so there's no reason to make A noncopyable.

like image 194
Puppy Avatar answered Oct 02 '22 15:10

Puppy