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

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

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

25条回答
  •  借酒劲吻你
    2020-11-22 01:08

    they're the same thing with different defaults (private by default for class, and public by default for struct), so in theory they're totally interchangeable.

    so, if I just want to package some info to move around, I use a struct, even if i put a few methods there (but not many). If it's a mostly-opaque thing, where the main use would be via methods, and not directly to the data members, i use a full class.

提交回复
热议问题