Class vs Struct for data only?

后端 未结 7 720
暖寄归人
暖寄归人 2020-11-28 15:35

Is there any advantage over using a class over a struct in cases such as these? (note: it will only hold variables, there will never be functions)

class Foo         


        
7条回答
  •  北海茫月
    2020-11-28 16:37

    Essentially the choice between a struct and a class comes down to your style and how much you want to type.

    • If you only have public members in a class/struct you might as well use the struct keyword. It will save you having to type out "public:" later on.
    • The other reason to choose a struct over a class would be to implicitly document the intent of the object. So you would make POD types structs (even if they contain a constructors and some static helper methods etc), and you would use class for all the other "regular" classes.

提交回复
热议问题