c++inheritanceimplementationnoncopyable

Does private inheritance always mean "HAS-A"?


According to my favorite author , Mr Scott Meyers, the private inheritance and composition means the same thing aka Has-A relationship. Thus everything that can be gained from composition (containment, when class A has class B as its member) can be gained by private inheritance, and visa-versa.

So the following code should be a Has-A relationship, but from my point of view, its not!

class A : private boost::noncopyable {.. this is irrelevant };

Can anyone please tell me that I am missing? Or how this code can be implemented through composition?


Solution

  • You example can be implemented through composition like this:

    class A {
    private:
        class B {
            B(const B&) = delete;
            B& operator=(const B&) = delete;
        } b;
    };
    

    A is noncopyable, because its member b is noncopyable.