简体   繁体   English

为什么在C ++中不允许初始化整数成员变量(不是const static)?

[英]Why is initialization of integer member variable (which is not const static) not allowed in C++?

My C++ compiler complains when i try to initialize a int member variable in class definition. 当我尝试在类定义中初始化int成员变量时,我的C ++编译器会抱怨。 It tells "only static const integral data members can be initialized within a class". 它告诉“只能在类中初始化静态const积分数据成员”。 Can you please explain the rationale behind this restriction (if possible with example). 能否请您解释这一限制背后的基本原理(如果可能,请举例说明)。

Because it's not allowed in the current standard. 因为在当前标准中不允许这样做。 According to Bjarne , you will be able to do this in C++0x. 根据Bjarne的说法 ,您将能够在C ++ 0x中执行此操作。 If you really need it, try setting the compiler to C++0x ( -std=c++0x in GCC) and see if your compiler supports it. 如果您真的需要它,请尝试将编译器设置为C ++ 0x(GCC中的-std=c++0x ),看看您的编译器是否支持它。

The rationale is the "low-level" nature of C++. 理由是C ++的“低级”性质。 If it would allow this, the compiler would need to generate initialization code for all constructors which is not entirely clear to the developer. 如果允许这样,编译器将需要为所有构造函数生成初始化代码,这对开发人员来说并不完全清楚。

After all it might be necessary to initialize members of base classes on the construction of a derived class even when the base class constructors are not explicitly invoked. 毕竟,即使未显式调用基类构造函数,也可能需要在构造派生类时初始化基类成员。

Static const integral variables do not need intitalization upon object creation. 静态const积分变量在创建对象时不需要初始化。

The static restriction exists because C++ uses constructor initializers to initialize non-static data members: 存在静态限制是因为C ++使用构造函数初始值设定项来初始化非静态数据成员:

struct Example {
  int n;
  Example() : n(42) {}
};

The const restriction exists because the const case is treated specially (rather than the other way around) so that static const integral members can usually be treated as if they had internal linkage, similar to const variables at namespace scope (C++03 §7.1.5.1p2, if you're interested). 存在const限制是因为const case是专门处理的(而不是相反),因此静态const积分成员通常可以被视为具有内部链接,类似于命名空间范围内的const变量(C ++03§7.1) .5.1p2,如果你有兴趣的话)。 This is primarily beneficial to use the members in integral constant expressions, such as array sizes. 这主要有利于在整数常量表达式中使用成员,例如数组大小。

I'm just guessing you're trying to do this: 我猜你正在尝试这样做:

class foo {
    int m_iX = 5;
};

This would require code to be run in the constructor, since every newly created instance would need to initialize this variable. 这将需要在构造函数中运行代码,因为每个新创建的实例都需要初始化此变量。 In C++, all code that is run during the constructor is (luckily) contained in the constructor itself, so it is immediately obvious what the construction of the class entails. 在C ++中,构造函数中运行的所有代码(幸运地)都包含在构造函数本身中,因此很明显,类的构造需要什么。 Furthermore, since a class can have any number of constructors (including copy constructors), it would be ambiguous as when this initialization should or should not take place. 此外,由于类可以包含任意数量的构造函数(包括复制构造函数),因此在进行或不应该进行此初始化时,这将是不明确的。

You can do this: 你可以这样做:

class foo {
    enum {
       CONSTANT = 8
    };
};

This allows you to use foo::CONSTANT . 这允许您使用foo::CONSTANT That works since it will be per-class rather than per-instance. 这是有效的,因为它将是每个类而不是每个实例。

Likewise, you can do this: 同样,你可以这样做:

class foo {
    static int sm_iX;
};

in the .cpp: 在.cpp中:

int foo::sm_ix = 5;

Again, this is per-class, not per-instance, and as such not relevant to the construction of an actual instance. 同样,这是每个类,而不是每个实例,因此与实际实例的构造无关。

Bonus - if you declare this int const, many compilers might evaluate it at compile-time. 奖金 - 如果你声明这个int const,很多编译器可能会在编译时对它进行评估。

Arun, 阿伦,

I believe your question is related to 我相信你的问题与你有关
Compiler Error C2864 编译器错误C2864

To achieve what you want to do, C++ requires you to initialize instance specific members (ie: non static, non cost) either in Constructor body or the initialization list. 为了实现您想要做的事情,C ++要求您在构造函数体或初始化列表中初始化特定于实例的成员(即:非静态,非成本)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM