1

The following code is giving me the error that 'A' is an inaccessible base of 'B' and I am not sure why:

class A {};
class B : protected A {};
A foo( A a ); 
///
B b; 
foo(b);

Any explanation for this much appreciated.

Edit: I suppose I am just confused about the nature of protected inheritance. I thought it meant that any derived class (in this case of A) could inherit its variables and its functions.

IntegrateThis
  • 853
  • 2
  • 16
  • 39
  • What did you expect with `protected` inheritance? – TartanLlama Oct 05 '16 at 14:51
  • @TartanLlama edited to explain my confusion. – IntegrateThis Oct 05 '16 at 14:52
  • 1
    http://stackoverflow.com/a/1372858/3751615 best explanation i have found for this concept – Rohith R Oct 05 '16 at 14:56
  • `B` will inherit all of `A`'s members (data or functions) regardless of the type of inheritance. What changes is the *access* to the inherited members. – juanchopanza Oct 05 '16 at 14:56
  • 2
    These duplicates answer the visibility for data members but not why a `B` can't be passed to a function that wants an `A` when `B` inherits from `A` through `protected`. Voting to reopen. – Hatted Rooster Oct 05 '16 at 14:58
  • @GillBates can you elaborate why this is true. – IntegrateThis Oct 05 '16 at 15:00
  • 2
    Basically because `foo` doesn't have access to `A` through a `B`. – Hatted Rooster Oct 05 '16 at 15:01
  • 1
    I agree with GillBates, some answers in the duplicate target skim over the reason why this happens, but it isn't a duplicate. – Vincent Savard Oct 05 '16 at 15:06
  • @Barry It would be nice to have your input on this since you single-handedly closed the question. – Vincent Savard Oct 05 '16 at 15:10
  • 1
    I have actually used protected inheritance in production code. Quite rare but it solved the particular problem very well. – CashCow Oct 05 '16 at 15:10
  • I have one of those gold C++ badges too but I agree that this is well-enough answered in the linked question. – CashCow Oct 05 '16 at 15:14
  • 1
    @CashCow I think we disagree on what a duplicate target should be. Just because the answers of one question happen to answer another question does not make both of them a duplicate. The duplicate target answers the different between the three different inheritances while the current question is basically "When using protected inheritance, why can't we substitute a variable of the parent class with the child class?". – Vincent Savard Oct 05 '16 at 15:16
  • @VincentSavard [This answer](http://stackoverflow.com/a/1374362/2069064). – Barry Oct 05 '16 at 15:17
  • 1
    @Barry I posted a comment at the same time as yours which explains why I disagree that they're duplicate. – Vincent Savard Oct 05 '16 at 15:17
  • @VincentSavard Yeah, and a consequence of the difference between the three different inheritances is WHY OP's code fails. That's the underlying answer. If you feel the answers to THAT question are insufficient, feel free to add your own. This question is just a minor subset of that one. – Barry Oct 05 '16 at 15:20
  • 1
    @Barry "This question is just a minor subset of that one.", which is exactly why they're not duplicates. I'm not going to argue more about this, but I find it sad to see so many questions being closed as duplicate targets when it's arguable that they are not, especially when it's closed with the dupe-hammer and the community doesn't have the time to actually vote on this. – Vincent Savard Oct 05 '16 at 15:21
  • 1
    @VincentSavard Feel free to post on meta if you feel strongly about it. A dupe doesn't mean literally the exact same question. That's far too narrow. It means that the information presented in the old question answers the new question. – Barry Oct 05 '16 at 15:34

1 Answers1

2

A class that inherits the protected parent class can get to it, otherwise it is considered private:

class A {};
class B : protected A {};
A foo(A a) { return a; };
class C : public B {
public:
    A foo(C c) { return c; };

};

int main() {
    B b;
    //foo(b); // Can't implicitly convert, A is protected (might as well be private from this line's perspective)
    C c;
    A a = c.foo(c); // class C can get A
}
wally
  • 10,717
  • 5
  • 39
  • 72
  • It works as an example although C::foo is unrelated to free-function foo, albeit it shows the cast from C to A even though it slices in so-doing. – CashCow Oct 05 '16 at 15:08