principlecppCritical
When should you use a class vs a struct in C++?
Viewed 0 times
youstructshouldclassusewhen
Problem
In what scenarios is it better to use a
struct vs a class in C++?Solution
The differences between a
Both classes and structs can have a mixture of
I would recommend you:
class and a struct in C++ are:structmembers and base classes/structs arepublicby default.
classmembers and base classes/structs areprivateby default.
Both classes and structs can have a mixture of
public, protected and private members, can use inheritance, and can have member functions.I would recommend you:
- use
structfor plain-old-data structures without any class-like features;
- use
classwhen you make use of features such asprivateorprotectedmembers, non-default constructors and operators, etc.
Context
Stack Overflow Q#54585, score: 1139
Revisions (0)
No revisions yet.