This question already has an answer here:
Is there ever an advantage of declaring a struct in C++? Why shouldn't I just make a class consisting only of data members(i.e. no methods)?
Thanks,
This question already has an answer here:
Is there ever an advantage of declaring a struct in C++? Why shouldn't I just make a class consisting only of data members(i.e. no methods)?
Thanks,
My favorite reason is inheritance. consider this code:
Now consider this very example with a struct:
Some say C++ is too verbose, but when checking their code, it look much more like the first example. I find
struct
much easier to read thanclass
. In my project I use struct everywhere.The question you should ask is "Why should I even consider using classes in C++?", because IMO, struct are the same but less verbose.
When you have a POD type where everything is public is saves a line...
vs
And it's also common practice for objects which are just dumb containers of things. Dumb meaning no constructors, operators, methods.