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

后端 未结 25 1653
误落风尘
误落风尘 2020-11-22 00:18

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

25条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 00:41

    They are pretty much the same thing. Thanks to the magic of C++, a struct can hold functions, use inheritance, created using "new" and so on just like a class

    The only functional difference is that a class begins with private access rights, while a struct begins with public. This is the maintain backwards compatibility with C.

    In practice, I've always used structs as data holders and classes as objects.

提交回复
热议问题