What's the most interesting wrong view people have on the difference between structure and class in C++?
-
4Stack Overflow is designed to support *right* answers, not wrong ones. – Greg Hewgill Jun 30 '10 at 20:03
-
That using the 'struct' keyword rather than 'class' can act as some kind of documentation to take the place of real documentation. – Edward Strange Jun 30 '10 at 20:08
-
That the difference between them is worth discussing. – Mike Seymour Jun 30 '10 at 20:19
3 Answers
For those who are interested in what the actual difference is, the default access specified for structs is public, and for classes it is private. There is no other difference.
See this related answer.
Member of a class defined with the keyword class are private by default. Members of a class defined with the keywords struct or union are public by default.
In absence of an access-specifier for a base class, public is assumed when the derived class is declared struct and private is assumed when the class is declared class.
I can imagine a lot of people thinking that there will be a performance difference but there is not.

- 1
- 1

- 811,555
- 193
- 1,581
- 1,452
It is very easy to assume that a struct
cannot have methods. This is false; I will sometimes put a constructor on a struct, for example.

- 299,747
- 42
- 398
- 622