什么时候在c ++中使用struct over class [英] When to use struct over class in c++

查看:117
本文介绍了什么时候在c ++中使用struct over class的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:


在使用C ++编写程序时,是否应该使用STRUCT声明而不是CLASS声明?


  1. 从不!


解决方案 / div>

两者之间的唯一区别是默认情况下 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++?

  1. Never !! ?
  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆