Class vs Struct for data only?

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

    If the contents of the type have no memory allocation issues (such as plain int), then using struct is fine if that's the way you want to go and you've made a conscious decision about it that you can justify to those who use your code. However, if any of the members is a pointer type, then you need to think hard about the memory management issues. It may still be OK to use a struct, but you are much more likely to need a destructor, and some constructors, and so on. At that point, you want a class.

    0 讨论(0)
提交回复
热议问题