0
votes

I have encountered the following response in a thread :

Protected members can be accessed from derived classes. Private ones can't.

class Base {

private: 
  int MyPrivateInt;
protected: 
  int MyProtectedInt;
public:
  int MyPublicInt;
};

class Derived : Base
{
public:
  int foo1()  { return MyPrivateInt;} // Won't compile!
  int foo2()  { return MyProtectedInt;} // OK  
  int foo3()  { return MyPublicInt;} // OK
};

class Unrelated 
{
private:
  Base B;
public:
  int foo1()  { return B.MyPrivateInt;} // Won't compile!
  int foo2()  { return B.MyProtectedInt;} // Won't compile
  int foo3()  { return B.MyPublicInt;} // OK
};

...

1) my question is: I have read : "A class derivation list names one or more base classes and has the form:

class derived-class: access-specifier base-class

Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default. " and "Private Inheritance: When deriving from a private base class, public and protected members of the base class become private members of the derived class.

"

SO...in our example class Derived : Base is equivalent to class Derived : private Base because no access-specifier has been defined, yet the code works as the writer said, so what am i missing?- i thought that Base class for the class Derived access-specifier is private therefore public and protected members of Base should be private for class Derived and can't be accessed... thanks!

1
You're mixing up concepts. There are two different relevances to access specifiers. The first is related to methods, which is what classes can "see" which base class methods. The second is related to inheritence, which would affect behaviors such as casting to base/derived classes. - Cory Kramer
Base is a private base class of Derived. A class can access its own bases (just like its own members) regardless of whether they are private, public, or protected. (Otherwise a private base class would be quite a useless thing). Other classes cannot acces Base through a Derived object because it's private. - n. 1.8e9-where's-my-share m.
note that, despite private being the default, "inheritance" commonly refers to public inheritance - 463035818_is_not_a_number

1 Answers

0
votes

Its a similar kind of idea. Rather than applying to which members of the class you can access, it applies to which base classes you can access.

class A
{
public:
    void f();
};



class B : public A
{
public:
    void g()
    {
        f(); // OK
        A *a = this; // OK
    }
};
class B2 : public B
{
public:
    void h()
    {
        f(); //OK
        A *a = this; // OK
    };
};
B b;
A& ba = b;


class C : protected A
{
public:
    void g()
    {
        f(); // OK, can access protected base
        A *a = this; // OK, can access protected base
    }
};
class C2 : public C
{
public:
    void h()
    {
        f(); // OK, can access protected base
        A *a = this; // OK, can access protected base
    };
};
C c;
c.f(); // Not OK, allthough A::f() is public, the inheritance is protected.
A& ca = c; // Not OK, inheritence is protected.




class D : private A
{
public:
    void g()
    {
        f(); // OK because A is a base of D
        A *a = this;
    }
};
class D2 : public D
{
public:
    void h()
    {
        f(); //Not OK, A is inherited with private in D
        A *a = this; //Not OK
    };
};
D d;
d.f(); // Not OK, allthough A::f() is public, the inheritance is private.
D& da = d; // Not OK, inheritence is private.