Skip to content

Last updated:2026-10-13

C++ struct vs class: Key Differences

About the Author:

Personal WeChat official account:

C++ struct vs class: Key Differences Explained | C++那些事

About struct in C and C++, see All About struct

Generally speaking, a struct is better seen as the implementation body of a data structure, while a class is better seen as the implementation body of an object.

Differences:

The most essential difference is the default access control

The default inheritance access permission: for a struct it is public, for a class it is private.

As the implementation body of a data structure, a struct's default data access control is public, whereas as the implementation body of an object, a class's default member variable access control is private.

FAQ

What is the only difference between struct and class?

Default access and default inheritance: struct defaults to public, class to private. Beyond that the two are fully equivalent in C++.

Can a struct have constructors and virtual functions?

Yes. A C++ struct is a class; it supports constructors, destructors, virtual functions and templates.

When should I use struct?

By convention, use struct for plain data aggregates with no invariants; switch to class once encapsulation or invariants appear.

Comments