0

I was looking at this question below at the back of my book and it got me a little confused. this would be false right?

In private inheritance, all public and protected members of the base class can be accessed in the derived class, but are not accessible through the derived class.

user3225981
  • 107
  • 7
  • 2
    It sounds mainly true (depending on the meaning of "through the derived class".) Why do you think it is false? – juanchopanza Mar 21 '15 at 22:30
  • Why do you think this would be false? Is that not what should happen? – BitTickler Mar 21 '15 at 22:31
  • I repeated this plenty of times as a comment on SO: if you have any questions about how public/protected/private inheritance works, this is THE link: http://stackoverflow.com/q/860339/3093378 (check the most voted answer) The only type of inheritance I cannot get (and I truly think is more than obscure) is `protected` inheritance. Maybe someone can illuminate me on why would it be useful. I am voting to close your question as afaik it is a duplicate of http://stackoverflow.com/q/860339/3093378 – vsoftco Mar 21 '15 at 22:53

3 Answers3

1

This is true. The subclass can use base class methods internally. But the methods of the base class would not be accessible from an instance of the derived class.

void Subclass::baseClassMethodOverride()
{
    useBaseClassMethod(); //<---- ALLOWED
}

int main()
{
    SubClass instance;
    instance.useBaseClassMethod(); //<-----NOT ALLOWED
}
davepmiller
  • 2,620
  • 3
  • 33
  • 61
1

That's true.

If you have a public or protected member you can access them in the derived class irregardless of the access modifier of the inheritance

dynamic
  • 46,985
  • 55
  • 154
  • 231
1

That is by definition what private inheritance is. For example, N4140 explains:

§11.2/1 [...] If a class is declared to be a base class for another class using the private access specifier, the public and protected members of the base class are accessible as private members of the derived class115.

Furthermore,

§11/1 A member of a class can be

private; that is, its name can be used only by members and friends of the class in which it is declared.

The phrase "but are not accessible through the derived class." is a little vague, but if the intended meaning is "can't be accessed outside of the derived class except for friends", then it's true.

Note: read the access specifier as appertaining to inheritance. i.e., private inheritance means what it inherits is private, not that it inherits private members.