在 C++ 为什么语言不强制对类/结构的公共、私有和受保护成员进行分组?
是否只允许逻辑分组?
Is it only to allow logical grouping?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
是否只允许逻辑分组?
Is it only to allow logical grouping?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
它给你灵活性。 例如,您可能有一堆构造函数,一些是公共的,一些是受保护的,一些是私有的 - 难道您不希望它们全部组合在一起吗?
It gives you flexibility. For example, you might have a bunch of constructors, some public, some protected, some private - wouldn't you want them all grouped together?
你为什么要强迫它? 它对编译器没有任何帮助,客观上也不会让人们更容易阅读。 C/C++ 哲学的一部分是,该语言不会制定无法实现某种特性/功能的任意规则。
它确实使代码生成变得更加容易。 许多编码风格在每个类中多次使用访问说明符 - 首先定义所有局部类型,然后是所有构造函数,然后是所有方法,然后是所有实例变量,等等...
C++ 给了你足够的绳索搬起石头砸自己的脚,但正是这种灵活性让您可以构建优雅、可维护且抽象化的应用程序。
Why would you force it? It doesn't help the compiler out at all, it doesn't make things objectively easier for a person to read. Part of the C/C++ philosophy is that the language doesn't make arbitrary rules that don't enable some sort of feature/functionality.
It does make things MUCH easier for code generation. Many coding styles use access specifiers more than once per class - first defining all the local types, then all constructors, then all the methods, then all the instance variables, etc...
C++ gives you enough rope to shoot yourself in the foot, but it's that same flexibility that lets you build elegant, maintainable, and well abstracted applications.
我认为你是对的。 不强制使用它可以让用户按照他们认为合适的方式对事物进行分组,以获得更好的代码可读性。
编译器可能会在内存中以不同的方式组织事物。
编辑:根据规范:
我在 一个相关的SO问题
I think you are correct. Leaving it unforced allows users to group things as they see fit for better code readability.
The compiler may organize things differently in memory.
edit: as per the spec:
I found this information in a related SO question
我的猜测是,它是 C 哲学的产物,它假设您知道自己在做什么并为您提供最大的灵活性。 这就像在 if 语句中允许使用单个 = 一样。
My guess is that it is an outgrowth of the C philosophy, which assumes that you know what you are doing and gives you the maximum flexibility. It is like allowing a single = in an if statement.
实际上,我以一种有点不愉快的方式利用了这一点:我经常使用的代码习惯用法是具有公共访问器函数的私有成员。
我有一个宏(不寒而栗),它可以从一行自动生成这些内容。
示例:
...生成:...
只要您记住 PROPERTY 宏会切换当前的可见性,这就可以正常工作,这并不令人愉快...
例如:
I actually take advantage of this in a slightly unpleasant way: A code idiom I often use is a private member, with public accessor functions.
I have a MACRO (shudder) which automatically generates these from a single line.
example:
...generates:...
This works fine as long as you remember that the PROPERTY macro switches the current visiblity, which is not pleasant....
eg:
Stroustrup 在《C++ 编程语言,第 3 版》中表示,这是为了使代码生成更容易。
尽管实际二进制文件中每个字段的位置基于该字段在源代码中声明的顺序确实有意义,因此这允许某人保持与 C 甚至其他语言/规范的某种布局兼容性。
In "The C++ Programming Language, 3rd edition," Stroustrup says this is to make code generation easier.
Although it does make sense that the position of each field in the actual binary is based on which order that field was declared in the source code, so this allows somebody to maintain some sort of layout compatibility with C or even other languages/specs.