Search code examples
c++inheritanceprivate

C++ turn public a variable after private extending


The doubt is as the title states:

With a class as follow:

class A
{
public:
    int a;
    int b;
    int c;

    function1();
    function2();
}

and a class B extending A, how to turn public all variables from A?

class B : private A
{
public:
    int a; //How to turn this public from A
    int b; //How to turn this public from A
    int c; //How to turn this public from A
}

Solution

  • Exposing the public data and members of a private base class as public data in a derived class is a bad idea. However, if you must, you can use:

    class B : private A
    {
       public:
          using A::a;
          using A::b;
          using A::c;
    };