Leonid Volnitsky
Leonid Volnitsky

Reputation: 9144

Using public keyword with struct

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?

Upvotes: 1

Views: 287

Answers (2)

juanchopanza
juanchopanza

Reputation: 227548

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.

Upvotes: 2

Griwes
Griwes

Reputation: 9039

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.

Upvotes: 4

Related Questions