Alan Hinchcliffe Alan Hinchcliffe - 1 year ago 74
C++ Question

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

In what scenarios is it better to use a

vs a
in C++?

Answer Source

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 and private members, can use inheritance and can have member functions. Classes create a namespace that also encapsulates the functions for manipulating its data elements. Classes may not be used when interfacing with C, because C does not have a concept of classes.

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download