Does the use of structs against classes have any impact in terms of speed, memory usage or efficiency?
No. There is no impact. Not even compile time differences.
Also, if you want to be sure of the performance impact of something, you should measure, and benchmark. If you don't, you won't ever know. Asking on StackOverflow does not replace measuring your own program.
Note that creating a correct benchmark that measure the right things take skills, just as writing good unit tests that helps you finding bugs without slowing you down.
You can also look at the assembly output of your compiler. As for me I definitely know there is no difference between the two because I observed no difference in the assembly output between the two, in all the cases I tried.
If there is a performance and memory usage impact, you should report the bug to your compiler implementer.
As you might know by now, struct
and class
are defined as being exactly the same, beside visibility. The sizeof
of a class is the same as the size of a struct with the same members. You can even have basic inheritance without overhead as compared to containing the type that would serve as a base. For example, std::tuple
has no overhead compared to a struct but cannot be implemented without inheritance.
However, as soon as you add a single virtual function or virtual inheritance, the compiler will add a vtable and RTTI. This comes as a pointer in your type that refer to that metadata. The vtable contains all the metadata to call virtual functions, check downcasting and sidecasting, virtual base location and type id.
So when to use struct and classes is entirely personal preference. For example I use struct
everywhere, because inheriting is public by default, and I put public members first.