c++c++11rule-of-zero

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.


Solution

  • 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.