什么时候在c ++中使用struct over class [英] When to use struct over class in c++
问题描述
在使用C ++编写程序时,是否应该使用STRUCT声明而不是CLASS声明?
- 从不!
两者之间的唯一区别是默认情况下 struct
是public的,而 class
的成员是private 。
我的规则是使用 struct
,当我的意思是一个相关数据的集合没有任何特殊语义的读/写他们。使用 class
,当我打算将数据包装在更有意义的更丰富的语义(读取方法),提供任何所需的保护或实现更抽象的东西。
Possible Duplicate:
When should you use a class vs a struct in C++?
When, if ever, should one use a STRUCT declaration rather than a CLASS declaration when writing a program using C++?
- Never !! ?
- Any thing...whichever makes one feel better?
The only difference between the two is that by default struct
are public while class
members are private.
My rule is to use struct
when I mean a clump of related data without any special semantics on reading/writing them. Use class
when I intend to wrap the data in richer semantics (read methods) that are more meaningful, provide any needed protection, or implement something more abstract.
这篇关于什么时候在c ++中使用struct over class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!