In what scenarios is it better to use a struct vs a class in C++?
In what scenarios is it better to use a struct vs a class in
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The differences between a
classand astructin 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,protectedandprivatemembers, can use inheritance, and can have member functions.I would recommend you:
structfor plain-old-data structures without any class-like features;classwhen you make use of features such asprivateorprotectedmembers, non-default constructors and operators, etc.