1

I understand that public/protected members are protected by protected inheritance, and they are private by private inheritance. But can I change them to public explicitly (as shown below)? I actually don't quite understand what it means by "public: A::x"....

    class A
{
public:
    int x;
    int y;
protected:
    int z;
};

class B : protected A   
{
public:
    A::x;
};

class C : private B 
{
public:
    B::y;
    B::z;
};
Sarah
  • 453
  • 7
  • 16

2 Answers2

1

The line

A::x;

is an "access declarator", so it indeed allows you to "make" an inherited member visible in the public area of the derived class. It is also used to make visible hidden overloaded functions. However it is now deprecated in C++, so try using

using A::x;

Example:

#include <iostream>

class Foo
{
protected:
    int x{10};
};

class Bar : public Foo
{
public:
    using Foo::x; // makes x "public" here
};

int main()
{
   Bar bar;
   std::cout << bar.x << std::endl;
}
Community
  • 1
  • 1
vsoftco
  • 55,410
  • 12
  • 139
  • 252
0
class B : protected A   
{
public:
    A::x;
};

This is not a valid syntax any more. This access declarator is deprecated now. To change accessibility of inherited member, you should use using keyword

class B : protected A   
{
public:
    using A::x;
};

Now, although x was inherited as protected, you explicitly chnged this to public access. So this is possible now:

B b;
b.x = 0; //Ok
b.y = 0; //Error, cannot access protected member
Mateusz Grzejek
  • 11,698
  • 3
  • 32
  • 49