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

Question:

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

Answer:

The only difference between a class and a struct in C++ is 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.

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.

上一篇:4年前端、2年CTO:一个非科班程序员的真实奋斗史


下一篇:linux驱动学习笔记---实现中断下半部以及驱动编写规范(七)【转】