Saturday, 30 September 2017

oop - When should you use a class vs a struct in C++?




In what scenarios is it better to use a struct vs a class in C++?


Answer



Differences between a class and a struct in C++ are that structs have default public members and bases and classes have default private members and bases. 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 using structs as plain-old-data structures without any class-like features, and using classes as aggregate data structures with private data and member functions.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...