0

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
}
Ricardo Alves
  • 1,071
  • 18
  • 36
  • 3
    Why would you even consider doing this? The whole point of access modifiers on your fields and functions is to hide implementation from users of the class. If you need to be able to modify private members from another class, access methods are usually provided for that very reason... I mention this because the answer to your question may become moot with a simple design consideration. – Ryan J Feb 09 '15 at 17:54
  • The point is that I have tons of public methods and other variables that cannot be turned public, and I only have three variables that should stay public – Ricardo Alves Feb 09 '15 at 17:55
  • 2
    You probably want to inherit `A` publicly, not privately. If that's not the case, this sounds like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Emil Laine Feb 09 '15 at 17:56

4 Answers4

7

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;
};
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • The only use I've really seen for this is to leverage an existing implementation without doing much work, e.g., your own vector safely derived from `std::vector` that can have simple `using` declarations instead of adapting each function. Whether or not that's better than using composition and adapting everything you need is debated. – chris Feb 09 '15 at 18:03
  • @chris, deriving from `std::vector` has its supporters and detractors :) – R Sahu Feb 09 '15 at 18:05
  • @RSahu. Of course. I don't think it would be the best example to collect those statistics from due to the oft-repeated "it's always unsafe to derive from anything without a virtual destructor" and "don't inherit from the containers", despite this usage being safe. – chris Feb 09 '15 at 18:08
2

Don't re-declare them in B.

Example:

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

    void function1() {  }
    void function2() {  }
};

// Use private inheritance
class B : private A {
public:
    using A::a;
    using A::b;
    using A::c;

private:
    void function3()
    {
        // 'a' exists here, as it is defined public in base class A
    }
};

int main()
{
    B b;
    b.a = 1;

    b.function1(); // error: ‘A’ is not an accessible base of ‘B’

    return 0;
}
Julian
  • 1,688
  • 1
  • 12
  • 19
0

Use other option for inheritance

  class B: public A

Read this Difference between private, public, and protected inheritance

Community
  • 1
  • 1
VolAnd
  • 6,367
  • 3
  • 25
  • 43
-1

I think you can do

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

Though, I never tried it;

murison
  • 3,640
  • 2
  • 23
  • 36