Search code examples
c++extend

C++ Extending Class Forcing some methods private


My question is simple: ~Given two classes, I want to one of them to extend the other one, but turning some methods to be private:

Class B
public Method a();
public Method b();
public Method c();

Class A extends B
private Method a();
private Method b();
public Method c();

Is this possible and how? Thanks!


Solution

  • This is what Private Inheritance is for.

    class A:   private B
    {
        // All methods of class B are now private.
        // To make some "public" again:
        public:
            Method c()  { return B::c(); } // Call the private c-method from class B.
    };