Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 733
Spide Avatar asked Jul 27 '26 06:07

Spide


1 Answers

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).

like image 82
Michael Kenzel Avatar answered Jul 29 '26 20:07

Michael Kenzel