Last updated:2026-10-13
C++ struct vs class: Key Differences¶
About the Author:¶
Personal WeChat official account:

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.
Related Topics¶
- C++ Pure Virtual Functions and Abstract Classes | C++那些事
- C++ assert | C++那些事
- C++ Bit Fields | C++那些事
- Implementing C++ Polymorphism in C | C++那些事
- C++ const Explained: Constants, const Pointer vs Pointer to const | C++那些事
- The Complete C++ const Family | C++那些事
- C++ Dereference Operator * | C++那些事
- C++ Initialization and Assignment | C++那些事
- Ways to Return Multiple Values in C++ | C++那些事
- C++ mutable Keyword | C++那些事