Class vs Struct for data only?

后端 未结 7 757
暖寄归人
暖寄归人 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:23

    There is no real advantage of using one over the other, in c++, the only difference between a struct and a class is the default visibility of it's members (structs default to public, classes default to private).

    Personally, I tend to prefer structs for POD types and use classes for everything else.

    EDIT: litb made a good point in the comment so I'm going to quote him here:

    one important other difference is that structs derive from other classes/struct public by default, while classes derive privately by default.

提交回复
热议问题