初始化静态成员时重新声明错误
Redeclaration error while initializing static member
我做了一个class,里面有一些静态成员。
我用 Google 搜索了 "how initializing static member",了解到我应该在 class 之外初始化静态成员。
但是,我在初始化静态成员时遇到重新声明错误。
我做错了什么?
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
using namespace std;
class Color
{
public:
int r,g,b;
Color();
Color(int r_,int g_,int b_)
{
r=r_;
g=g_;
b=g_;
}
static Color Red;
};
int main()
{
Color Color::Red=Color(255,0,0);
return 0;
}
尝试将其移出 main()
。
...
Color Color::Red = Color(255, 0, 0);
int main()
{
...
你应该像这样初始化你的静态成员。
class class_name {
static int i;
};
int class_name::i = 0;
这是必需的,因为您的 class 只能有一个 i 实例。
但是对于常量变量,您可以直接在 class.
中执行此操作
class class_name {
static int i;
const static int j = 20;
};
编辑:
标准的第 6.7 节是这样说的:
The zero-initialization of all local objects with static storage
duration is performed before any other initialization takes place. A
local object of POD type with static storage duration initialized with
constant-expressions is initialized before its block is first entered.
An implementation is permitted to perform early initialization of
other local objects with static storage duration under the same
conditions that an implementation is permitted to statically
initialize an object with static storage duration in namespace scope.
Otherwise such an object is initialized the first time control passes
through its declaration; such an object is considered initialized upon
the completion of its initialization. If the initialization exits by
throwing an exception, the initialization is not complete, so it will
be tried again the next time control enters the declaration. If
control re-enters the declaration (recursively) while the object is
being initialized, the behavior is undefined.
我做了一个class,里面有一些静态成员。
我用 Google 搜索了 "how initializing static member",了解到我应该在 class 之外初始化静态成员。
但是,我在初始化静态成员时遇到重新声明错误。
我做错了什么?
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
using namespace std;
class Color
{
public:
int r,g,b;
Color();
Color(int r_,int g_,int b_)
{
r=r_;
g=g_;
b=g_;
}
static Color Red;
};
int main()
{
Color Color::Red=Color(255,0,0);
return 0;
}
尝试将其移出 main()
。
...
Color Color::Red = Color(255, 0, 0);
int main()
{
...
你应该像这样初始化你的静态成员。
class class_name {
static int i;
};
int class_name::i = 0;
这是必需的,因为您的 class 只能有一个 i 实例。 但是对于常量变量,您可以直接在 class.
中执行此操作class class_name {
static int i;
const static int j = 20;
};
编辑: 标准的第 6.7 节是这样说的:
The zero-initialization of all local objects with static storage duration is performed before any other initialization takes place. A local object of POD type with static storage duration initialized with constant-expressions is initialized before its block is first entered. An implementation is permitted to perform early initialization of other local objects with static storage duration under the same conditions that an implementation is permitted to statically initialize an object with static storage duration in namespace scope. Otherwise such an object is initialized the first time control passes through its declaration; such an object is considered initialized upon the completion of its initialization. If the initialization exits by throwing an exception, the initialization is not complete, so it will be tried again the next time control enters the declaration. If control re-enters the declaration (recursively) while the object is being initialized, the behavior is undefined.