在 C++ 为什么语言不强制对类/结构的公共、私有和受保护成员进行分组?

发布于 2024-07-08 23:16:54 字数 17 浏览 9 评论 0原文

是否只允许逻辑分组?

Is it only to allow logical grouping?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

青柠芒果 2024-07-15 23:16:55

它给你灵活性。 例如,您可能有一堆构造函数,一些是公共的,一些是受保护的,一些是私有的 - 难道您不希望它们全部组合在一起吗?

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?

○愚か者の日 2024-07-15 23:16:55

你为什么要强迫它? 它对编译器没有任何帮助,客观上也不会让人们更容易阅读。 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.

坦然微笑 2024-07-15 23:16:55

我认为你是对的。 不强制使用它可以让用户按照他们认为合适的方式对事物进行分组,以获得更好的代码可读性。

编译器可能会在内存中以不同的方式组织事物。

编辑:根据规范:

§9.2第12条(1998年和2003年标准):

在没有介入访问说明符的情况下声明的(非联合)类的非静态数据成员是分配以便后来的成员在类对象中具有更高的地址。 由访问说明符分隔的非静态数据成员的分配顺序未指定 (11.1)。 实现对齐要求可能会导致两个相邻成员不能立即分配; 管理虚拟函数 (10.3) 和虚拟基类 (10.1) 的空间要求也可能如此。

我在 一个相关的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:

§9.2 clause 12 (1998 and 2003 standards):

Nonstatic data members of a (non-union) class declared without an intervening access-specifier are allocated so that later members have higher addresses within a class object. The order of allocation of nonstatic data members separated by an access-specifier is unspecified (11.1). Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions (10.3) and virtual base classes (10.1).

I found this information in a related SO question

追风人 2024-07-15 23:16:55

我的猜测是,它是 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.

小猫一只 2024-07-15 23:16:55

实际上,我以一种有点不愉快的方式利用了这一点:我经常使用的代码习惯用法是具有公共访问器函数的私有成员。

我有一个宏(不寒而栗),它可以从一行自动生成这些内容。

示例:

PROPERTY(int, MyVal);

...生成:...

private:
  int fMyVal;
public:
  void setMyVal(const int f) { fMyVal = f; };
  int getMyVal() { return fMyVal; };

只要您记住 PROPERTY 宏会切换当前的可见性,这就可以正常工作,这并不令人愉快...

例如:

protected:
  int v1;
  PROPERTY (int, v2) // fv2 is  private with public accessors
  int v3;  // whoops. f3 is public,

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:

PROPERTY(int, MyVal);

...generates:...

private:
  int fMyVal;
public:
  void setMyVal(const int f) { fMyVal = f; };
  int getMyVal() { return fMyVal; };

This works fine as long as you remember that the PROPERTY macro switches the current visiblity, which is not pleasant....

eg:

protected:
  int v1;
  PROPERTY (int, v2) // fv2 is  private with public accessors
  int v3;  // whoops. f3 is public,
寂寞笑我太脆弱 2024-07-15 23:16:55

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文