1

Suppose I have the following code:

class A
    {
    public:
        void foo() const {}
    };

    class B : protected A
    {
    public:
    void print() const
    {
        foo();
    }
    };

    void main()
    {

    B b;
    b.print();
    b.foo();
    }

Now, by reading Difference between private, public, and protected inheritance, I conclude that in case of protected inheritance, every public member of the base (for that matter - class A) will be acsessible in the derived class (class B).

However, I dont understand why the command b.foo(); is not allowed in this case, becuase it apparently seems to be allowed according to the rules of protected inheritance.

DonaldT
  • 103
  • 8

2 Answers2

0

You are trying to access B::foo() from the scope of main(). As foo is not public in this context, it's not allowed.

pablo285
  • 2,460
  • 4
  • 14
  • 38
-1

In this case, only the class B "knows" or "is aware" of the inherent relationship between class A and itself due to the relationship being protected. Namely, main() doesn't know of said relationship.

Anzurio
  • 16,780
  • 3
  • 39
  • 49