c++inheritance

C++ multiple inheritance accessmodifier


class A{...}
class B{...}
class C : public A, B {...}

Are A and B now both public inherited? Or do I have to write an explicit access modifier to every class?


Solution

  • The syntax for the base class list is basically (full grammatical specification in [class.derived] §1):

    [private|protected|public] [virtual] <base-class-name>, ...
    

    So the access specifier is part of each base specifier.

    class C : public A, B {...}
    

    Here, only A has an explicit access specifier public, the other base B has not explicit access specifier given, which means the default will be used. Since C is declared with the class-key class, the default base access specifier will be private (see [class.access.base] §2).