如何在 C++ 中初始化静态常量成员?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3531060/
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
How to initialize a static const member in C++?
提问by anarhikos
Is it possible to initialize a static const value outside of the constructor? Can it be initialized at the same place where member declarations are found?
是否可以在构造函数之外初始化静态常量值?它可以在发现成员声明的同一个地方初始化吗?
class A {
private:
static const int a = 4;
/*...*/
};
回答by Klaim
YES you can but only for int types. If you want your static member to be any other type, you'll have to define it somewhere in a cpp file.
是的,您可以,但仅适用于 int 类型。如果您希望您的静态成员是任何其他类型,则必须在 cpp 文件中的某处定义它。
class A{
private:
static const int a = 4; // valid
static const std::string t ; // can't be initialized here
...
...
};
// in a cpp file where the static variable will exist
const std::string A::t = "this way it works";
Also, note that this rule have been removed in C++11, now (with a compiler providing the feature) you can initialize what you want directly in the class member declaration.
另请注意,此规则已在 C++11 中删除,现在(使用提供该功能的编译器)您可以直接在类成员声明中初始化您想要的内容。
回答by anarhikos
Static data members (C++ only)
The declaration of a static data member in the member list of a class is not a definition. You must define the static member outside of the class declaration, in namespace scope. For example:
类成员列表中静态数据成员的声明不是定义。您必须在类声明之外的命名空间范围内定义静态成员。例如:
class X
{
public:
static int i;
};
int X::i = 0; // definition outside class declaration
Once you define a static data member, it exists even though no objects of the static data member's class exist. In the above example, no objects of class X exist even though the static data member X::i has been defined.
一旦定义了静态数据成员,即使静态数据成员的类的对象不存在,它也存在。在上面的例子中,即使定义了静态数据成员 X::i,也不存在类 X 的对象。
Static data members of a class in namespace scope have external linkage. The initializer for a static data member is in the scope of the class declaring the member.
命名空间范围内的类的静态数据成员具有外部链接。静态数据成员的初始值设定项在声明该成员的类的范围内。
A static data member can be of any type except for void or void qualified with const or volatile. You cannot declare a static data member as mutable.
静态数据成员可以是任何类型,除了用 const 或 volatile 限定的 void 或 void 。您不能将静态数据成员声明为可变的。
You can only have one definition of a static member in a program. Unnamed classes, classes contained within unnamed classes, and local classes cannot have static data members.
一个程序中只能有一个静态成员的定义。未命名类、未命名类中包含的类和本地类不能具有静态数据成员。
Static data members and their initializers can access other static private and protected members of their class. The following example shows how you can initialize static members using other static members, even though these members are private:
静态数据成员及其初始值设定项可以访问其类的其他静态私有成员和受保护成员。以下示例显示了如何使用其他静态成员初始化静态成员,即使这些成员是私有的:
class C {
static int i;
static int j;
static int k;
static int l;
static int m;
static int n;
static int p;
static int q;
static int r;
static int s;
static int f() { return 0; }
int a;
public:
C() { a = 0; }
};
C c;
int C::i = C::f(); // initialize with static member function
int C::j = C::i; // initialize with another static data member
int C::k = c.f(); // initialize with member function from an object
int C::l = c.j; // initialize with data member from an object
int C::s = c.a; // initialize with nonstatic data member
int C::r = 1; // initialize with a constant value
class Y : private C {} y;
int C::m = Y::f();
int C::n = Y::r;
int C::p = y.r; // error
int C::q = y.f(); // error
The initializations of C::p and C::q cause errors because y is an object of a class that is derived privately from C, and its members are not accessible to members of C.
C::p 和 C::q 的初始化会导致错误,因为 y 是从 C 私有派生的类的对象,并且 C 的成员无法访问其成员。
If a static data member is of const integral or const enumeration type, you may specify a constant initializer in the static data member's declaration. This constant initializer must be an integral constant expression. Note that the constant initializer is not a definition. You still need to define the static member in an enclosing namespace. The following example demonstrates this:
如果静态数据成员是常量整型或常量枚举类型,则可以在静态数据成员的声明中指定常量初始值设定项。这个常量初始化器必须是一个整数常量表达式。请注意,常量初始值设定项不是定义。您仍然需要在封闭的命名空间中定义静态成员。以下示例演示了这一点:
#include <iostream>
using namespace std;
struct X {
static const int a = 76;
};
const int X::a;
int main() {
cout << X::a << endl;
}
The tokens = 76 at the end of the declaration of static data member a is a constant initializer.
静态数据成员 a 声明末尾的标记 = 76 是一个常量初始值设定项。
回答by Chubsdad
Just for the sake of completeness, I am adding about the static template member variables.
为了完整起见,我添加了静态模板成员变量。
template<class T> struct X{
static T x;
};
template<class T> T X<T>::x = T();
int main(){
X<int> x;
}
回答by sbi
You cannot initialize static members within constructors. Integral types you can initialize inline at their declaration. Other static members must be defined (in a .cpp
) file:
您不能在构造函数中初始化静态成员。您可以在声明时内联初始化整数类型。其他静态成员必须(在.cpp
)文件中定义:
// .h
class A{
private:
static const int a = 4;
static const foo bar;
...
...
};
// .cpp
const foo A::bar = ...;