为什么我们不能初始化结构体中的成员?
为什么我们不能初始化结构体中的成员?
例子:
struct s {
int i = 10;
};
Why can't we initialize members inside a structure ?
example:
struct s {
int i = 10;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您想在
struct
声明中初始化非静态成员:在C++(不是C)中,
struct
是几乎与类同义,并且可以在构造函数中初始化成员。如果你想初始化一个实例:
在C或C++中:
C99还有一个称为指定初始化器的功能:
还有一个GNU C扩展,它与C99指定初始化器非常相似,但最好使用更便携的东西:
If you want to initialize non-static members in
struct
declaration:In C++ (not C),
structs
are almost synonymous to classes and can have members initialized in the constructor.If you want to initialize an instance:
In C or C++:
C99 also has a feature called designated initializers:
There is also a GNU C extension which is very similar to C99 designated initializers, but it's better to use something more portable:
直接的答案是因为结构体定义声明了一个类型,而不是一个可以初始化的变量。 你的例子是:
这没有声明任何变量 - 它定义了一个类型。 要声明变量,您需要在
}
和;
之间添加一个名称,然后再初始化它:正如 Checkers 所指出的,在 C99 中,您还可以使用指定的初始化器(这是一个很棒的改进——有一天,C 将赶上 Fortran 66 数据初始化的其他功能,主要是重复初始化器指定的次数)。 这种简单的结构没有任何好处。 如果您有一个包含 20 个成员的结构,并且只需要初始化其中一个成员(例如,因为您有一个标志表明该结构的其余部分已初始化或未初始化),那么它会更有用:
此表示法也可用于初始化联合,以选择初始化联合的哪个元素。
The direct answer is because the structure definition declares a type and not a variable that can be initialized. Your example is:
This does not declare any variable - it defines a type. To declare a variable, you would add a name between the
}
and the;
, and then you would initialize it afterwards:As Checkers noted, in C99, you can also use designated initializers (which is a wonderful improvement -- one day, C will catch up with the other features that Fortran 66 had for data initialization, primarily repeating initializers a specifiable number of times). With this simple structure, there is no benefit. If you have a structure with, say, 20 members and only needed to initialize one of them (say because you have a flag that indicates that the rest of the structure is, or is not, initialized), it is more useful:
This notation can also be used to initialize unions, to choose which element of the union is initialized.
请注意,在 C++ 11 中,现在允许以下声明:
这是一个老问题,但它在 Google 中排名很高,不妨澄清一下。
Note that in C++ 11, the following declaration is now allowed:
This is an old question, but it ranks high in Google and might as well be clarified.
Edit2: 这个答案写于 2008 年,与 C++98 相关。 成员初始化的规则在该语言的后续版本中发生了变化。
编辑:这个问题最初被标记为
c++
,但发帖者说它与c
有关,所以我重新标记了问题,我留下了答案不过...在 C++ 中,
struct
只是一个class
,其成员默认为public
而不是private
遗产。C++ 只允许内联初始化
static const
整型成员,其他成员必须在构造函数中初始化,或者如果struct
是 POD 在初始化列表中(声明变量时)。Edit2: This answer was written in 2008 and relates to C++98. The rules for member initialization have changed in subsequent versions of the language.
Edit: The question was originally tagged
c++
but the poster said it's regardingc
so I re-tagged the question, I'm leaving the answer though...In C++ a
struct
is just aclass
which defaults forpublic
rather thanprivate
for members and inheritance.C++ only allows
static const
integral members to be initialized inline, other members must be initialized in the constructor, or if thestruct
is a POD in an initialization list (when declaring the variable).我们无法初始化,因为当我们声明任何结构而不是我们实际执行的结构时,只需通知编译器它们的存在,即没有为其分配内存,并且如果我们初始化没有内存的成员。 通常,当我们初始化任何变量时会发生什么,这取决于我们声明变量的位置编译器为该变量分配内存。
那么需要什么内存来保存该数据,但在结构的情况下,没有内存,因此无法初始化它。
We can't initialize because when we declared any structure than actually what we do, just inform compiler about their presence i.e no memory allocated for that and if we initialize member with no memory for that. Normally what happens when we initialize any variable that depends on the place where we declared variable compiler allocate memory for that variable.
So what memory is required to hold that data but in case of structure no memory is there so not possible to initialize it.
正如你所说,它只是一个成员而不是一个变量。 当您声明变量时,编译器还将为这些变量提供可以放置值的内存空间。 对于结构成员的 a ,编译器不会为其提供内存空间,因此除非创建该结构类型的变量,否则无法为结构成员赋值。
As you said it's just a member not a variable. When you declare a variable the compiler will also provide memory space for those variables where you can put values. In the case a of a struct member the compiler is not giving memory space for it, so you cannot assign values to struct members unless you create a variable of that struct type.