1

Is there a difference between:

struct B {...}; 
struct D : B {...};

and:

struct B {...};  
struct D : public B {...};

If no, why everyone, including the standard, using more verbose, 2nd variant?

TemplateRex
  • 69,038
  • 19
  • 164
  • 304
Leonid Volnitsky
  • 8,854
  • 5
  • 38
  • 53

2 Answers2

4

No, because with structs, both default access specifier and default type of inheritance is public. Speaking of that, default access specifier for classes is private, and that's the only difference between struct and class in C++ in context of declaring types.

As for "why" - probably because it's a bit more explicit and one doesn't need to remember the difference between class and struct in that matter.

Griwes
  • 8,805
  • 2
  • 43
  • 70
  • 2
    "and that's the only difference between struct and class in C++" in the context of struct/class declarations. There is an unrelated difference, which is that `class` has another meaning related to templates, which `struct` doesn't. –  Aug 12 '12 at 11:44
2

No, the two are exactly the same. And I am not sure everyone uses the second variant. I don't, for instance. Neither does the standard use it everywhere. For example, in C++11 §3.8, we see this:

struct D1 : B { void f(); };

On the other hand, being explicit means that people who don't know the rules can understand the code.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480