-1

When I write

class Derived : Base { };

This compiles. (I sort of assumed that one needs to specify it as being one of public, protected, or private inheritance, which has consequences about the visibility of members.

Which one of the three is it if I do not specify?

Steven Lu
  • 41,389
  • 58
  • 210
  • 364
  • Please explain downvote so I can improve the question. – Steven Lu Apr 09 '15 at 01:19
  • 1
    It seems like this should be explained in any C++ textbook or tutorial. – Barmar Apr 09 '15 at 01:19
  • I get answers when Googling the title. – chris Apr 09 '15 at 01:20
  • Nope, I have found a large number of them and all explicitly write out which type – Steven Lu Apr 09 '15 at 01:20
  • Duplicate of: http://stackoverflow.com/q/4796789/1702990 Literally the first result when you google "c++ default inheritance access" – Sinkingpoint Apr 09 '15 at 01:20
  • Cool. incidentally I did find an "answer" in [this](http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/) article, which turns out to be wrong. it depends on what type of inheritance Base uses – Steven Lu Apr 09 '15 at 01:22
  • 3
    @StevenLu that article says "If you do not choose an inheritance type, C++ defaults to private inheritance" . (which is correct). The line which says "the access specifiers may change depending on the method of inheritance" is talking about the access specifiers of the base class's members when they are inherited as part of a derived class. – M.M Apr 09 '15 at 01:23
  • @MattMcNabb Much obliged for your correction. This is what I get for trying to rush things... – Steven Lu Apr 09 '15 at 01:32

2 Answers2

2

The default is "private" for classes, and "public" for structs. This is also true for the default access mode of members in said classes and structs.

Cort Ammon
  • 10,221
  • 31
  • 45
1

In a class, members are by default private; in a struct, members are by default public (§16.2.4).