1
votes

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.

2
It might help to pretend B declares a protected: void foo() const;. The same access restrictions apply. - François Andrieux
The access rules state that B can access foo but in b.foo(); it's not B doing the accessing but you - NathanOliver
in is the keyword. The scope of main is not in there, is it? - StoryTeller - Unslander Monica

2 Answers

0
votes

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

-1
votes

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.