Is the size of a struct and the size of the equivalent class guaranteed to be equal?
-
8C does not have classes, maybe asking about CPP? – hahcho Aug 13 '14 at 16:22
-
3http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c – ktzhang Aug 13 '14 at 16:23
-
C struct and c++ class. Whats the difference between them apart from the access specifier ? I want in terms of their sizes? Like which will take more size.? – Thejas B Aug 13 '14 at 16:26
-
http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c – Cody Glickman Aug 13 '14 at 16:27
-
2This is not a duplicate given the added question about the size differences. Nominating for reopen. – Niels Keurentjes Aug 13 '14 at 16:36
-
@NielsKeurentjes I think the duplicate is valid. The differences are listed and explained in the duplicate. Size is not one of them. – juanchopanza Aug 13 '14 at 17:03
-
@juanchopanza The way I understand it, the standard does not specify the size of a struct or a class, but leaves them to the compiler to decide. Thus the standard could treat the size of a struct and the equivalent class the same way, i.e. unspecified, without requiring them to be equal. I think the question is valid. – Johan Råde Aug 13 '14 at 17:15
-
@user763305 That may have been the rule in pre-C++11 (although even then, you could define a class using the `class` keyword, and forward declare it using `struct`), but C++11 introduces the notion of layout compatibility, which restricts things greatly. – James Kanze Aug 13 '14 at 17:49
2 Answers
Assuming you're asking about CPP. Members of a struct are public by default. In a class, they default to private.
struct and class are otherwise functionally equivalent. structs are typically used as open data containers.
The size of a class and structure will tend to be the same given that the ordering of its data members are in the same order and you did not change the default member alignment.

- 31
- 6
-
How do you know they will be the same size? Does the standard explicitly guarantee that? – Johan Råde Aug 13 '14 at 16:31
-
Sizes will be the same if you don't change the default packing. I've edited my answer as the standard does not explicitly guarantee the size of a structure and class to be the same. – jormigo Aug 13 '14 at 16:38
-
@user3822244 The standard uses `class` and `struct` quite interchangeably. Out of the three *class-keys* the odd one out is `union`. The other two are basically the same. – juanchopanza Aug 13 '14 at 17:12
-
@juanchopanza I took another look at Section 6.7.2.1 of the C99 and you're right. unions have always been the odd ones. – jormigo Aug 13 '14 at 17:22
Essentially struct
and class
are identical in C++, with 2 differences:
- Members of a struct are
public
by default, while in a class they'reprivate
. Access modifiers still apply when supplied by the programmer. - When deriving a struct from a class or struct, the default access modifier for the base is
public
. And when deriving a class, the default access isprivate
.
In general, programmers assume that in a struct
all members are public and freely modifiable, and in a class
they will all be private and getters/setters will be present as appropriate. This is merely convention though and not enforced by the language.
Since they are treated identically by the compiler apart from the default access, sizes are guaranteed to be identical. The main reason that both keywords are historically supported by C++, even though slightly redundant, is backwards compatibility with C where only struct
exists.

- 41,402
- 9
- 98
- 136