What is the difference between struct and class in C++ beyond the default access specifier?
Last updated
The only language-level difference is the default access: members and base-class inheritance are public in a struct and private in a class. Otherwise they are fully interchangeable: both can have methods, constructors, inheritance, access specifiers, and templates.
Default member access:
struct: members default topublic.class: members default toprivate.
Default inheritance access:
struct B : Ais public inheritance;class B : Ais private inheritance.Everything else is identical: Both support methods, virtual functions, access control, templates, and constructors/destructors.
Conventional usage (not enforced):
structis typically used for passive aggregates of public data (POD-like);classfor types with invariants and encapsulation.