C++ 具有静态成员的静态结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14976971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:51:20  来源:igfitidea点击:

Static struct with static members

c++staticstruct

提问by Pavlo Dyban

Today I found myself creating a static array of 2 ints, and because its inline initalization is not allowed in C++ (not C++11), I reverted to using a static variable of type struct.

今天我发现自己创建了一个由 2 个整数组成的静态数组,并且因为它的内联初始化在 C++(不是 C++11)中是不允许的,所以我恢复到使用类型为 struct 的静态变量。

class MyWidget {
  ...
  static const struct Margin {
    const int horizontal = 1;
    const int vertical = 1;
  } margin;

};

I noticed that internal variables are used only once for all instances of struct Margin, so I decided to make them static too.

我注意到内部变量只对 struct Margin 的所有实例使用一次,所以我决定也将它们设为静态。

class MyWidget {
  ...
  static const struct Margin {
    static const int horizontal = 1;
    static const int vertical = 1;
  } margin;

};

What wonders me is the difference between declaring a static struct variable vs. a static struct variable with static members. AFAC static objects are allocated only once in memory, therefore Margin struct wil be allocated only once no matter if my members are static or not.

让我感到奇怪的是声明静态结构变量与具有静态成员的静态结构变量之间的区别。AFAC 静态对象在内存中仅分配一次,因此无论我的成员是否为静态,Margin 结构都将仅分配一次。

Do I miss something? Does there exist a difference or is it a mere syntactic sugar?

我错过了什么吗?是否存在差异或仅仅是语法糖?

回答by antonijn

You seem to be a bit confused about "static structs", because in C++, there are no such things as static structs (as opposed to languages like C#, where static classes are a workaround for the fact that there are no global functions).

您似乎对“静态结构”有些困惑,因为在 C++ 中,没有静态结构之类的东西(与 C# 等语言相反,其中静态类是解决没有全局函数这一事实的一种解决方法)。

What you're doing, is creating an instance of that class, and making the instance(margin) static (and constant). So your struct is not static, you are simply defining a struct, and making a static constinstance of it, belonging to MyWidget. The difference between the two given examples now, should be quite obvious.

您正在做的是创建该类的实例,并使实例( margin) 成为静态(和常量)。所以你的结构不是静态的,你只是定义一个结构,并创建static const它的一个实例,属于MyWidget. 现在给出的两个例子之间的区别应该是很明显的。

In the first example, you're creating a static variable called margin, belonging to MyWidget, meaning you can access the horizontalmember like so

在第一个示例中,您正在创建一个名为 margin 的静态变量,属于MyWidget,这意味着您可以horizontal像这样访问成员

MyWidget::margin.horizontal

Where marginis the instance you have created.

margin您创建的实例在哪里。

Whereas if you made the members of the struct static, you would not be able to do that. Instead, you would have to access them like so:

而如果您将结构体的成员设为静态,您将无法做到这一点。相反,您必须像这样访问它们:

MyWidget::Margin::horizontal

Where Marginis the struct. Note however, that in the second case, there is no need for the static instance margin, since it has no instance fields associated with it.

哪里Marginstruct。但是请注意,在第二种情况下,不需要静态 instance margin,因为它没有与之关联的实例字段。

回答by Arne Mertz

There is indeed a difference:

确实有区别:

class MyWidget {
  ...
  static const struct Margin {
    const int horizontal = 1;
    const int vertical = 1;
  } margin;


  void foo() {
    Margin anotherMargin = { 3, 4 };
  }
};

This creates another instance of Margin, having different members. The staticin static const struct Margin {...} margin;applies to the variable margin, not to the class/struct Margin. That means there is only one Margin object shared by all MyWidget objects, but you can very well create other Margin objects having different values. The above code would not compile with horizontaland verticalbeing static themselves, because then a Margin object would hav no member variables (statics are no real members) and therefore all Margin objects would share the horizontaland verticalvalues.

这将创建另一个 Margin 实例,具有不同的成员。将staticstatic const struct Margin {...} margin;适用于变量margin,而不是类/结构Margin。这意味着只有一个 Margin 对象被所有 MyWidget 对象共享,但您可以很好地创建其他具有不同值的 Margin 对象。上面的代码不会与编译horizontalvertical静态的自己,因为然后追加保证金对象将甲肝没有成员变量(静力学没有真正的成员),因此所有保证金对象将共享horizontalvertical值。

回答by lego

Yes, there are significant differences. In the both cases you declare MyWidget::marginthat is of type MyWidget::Marginand they are static. In first case marginis an object that has two fields, horizontaland vertical. In second case, marginis an object with no fields and you could just drop that object.

是的,存在显着差异。在这两种情况下,您都声明MyWidget::margin它是类型MyWidget::Margin并且它们是静态的。在第一种情况下margin是一个具有两个字段的对象,horizontalvertical。在第二种情况下,margin是一个没有字段的对象,您可以删除该对象。

In the first case you need to use form margin.vertical(or MyWidget::margin.verticalif accessing from outside MyWidget) to access the fields, in second case, you can do it like this Margin::vertical(or MyWidget::Margin::vertical).

在第一种情况下,您需要使用表单margin.vertical(或者MyWidget::margin.vertical如果从外部访问MyWidget)来访问字段,在第二种情况下,您可以这样做Margin::vertical(或MyWidget::Margin::vertical)。