在课堂上或课堂外放置枚举的地方? [英] Where to put enum, inside class or outside?

查看:93
本文介绍了在课堂上或课堂外放置枚举的地方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在哪里放一个枚举,在类定义/声明中或在它之外?

Where should one put an enum, inside class definition/declaration or outside of it?

推荐答案

在某种程度上它是个人偏好的问题,但是我的一般规则是如果它与班级相关,请把它放在课堂上



有理由在课堂上投入:

1)它通过以分层方式组织结构来减少名称空间的污染。

2)它将相关项目组合在一起。

3)它让你控制可以使用枚举的类的范围 - 如果他们不能访问引用它的函数或数据,那么他们就没有需要知道它的存在。



其他人会告诉你这意味着你在使用它时会更多打字:是的。这可能会在一定程度上降低可读性。但我认为这是一件好事,因为它可以确保您使用的是您认为自己的类/枚举,而不是来自不同来源的类似名称。
To an extent it''s a matter of personal preference, but my general rule is "If it''s class related, put it in the class"

There are reasons for putting in within the class:
1) It reduces the "Pollution" of the namespace by organising the structures in a hierarchical manner.
2) It groups related items together.
3) It lets you control the scope of classes which can use the enum - if they can''t access the functions or data which refer to it, then they have no "need to know" of it''s existence.

Others will tell you it means more typing when you use it: yes it does. And that can reduce readability to an extent. But I think that it''s a good thing, because it ensures that you are using exactly the class / enum you think you are, not a similarly named one from a different source.


您好,



这取决于枚举的使用方式。如果它只在一个类中使用,我会把它放到那个类中。否则,枚举应嵌套在不同的范围(例如命名空间,头文件)中,以便多个类/函数可以使用。



希望这会有所帮助。< br $>


祝你好运,

JK
Hi,

well it depends on how the enum is used. If it is used only throughout one class, I would put it into that class. Otherwise the enum shall be nested in a different scoped (e.g. namespace, header file) so that is can be used by multiple classes/functions.

Hope this helps.

Best regards,
J.K


这篇关于在课堂上或课堂外放置枚举的地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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