英文:
Seeking clarity regards C++ static member initialization
问题
I am quite confused regarding intialization and usage of C++ class/struct static members.
Let's say, that I have defined a struct MapMetaData
in a header file named Constants.h
. Here's how it looks -
struct MapMetaData {
static float cell_size_, origin_x_, origin_y_;
static unsigned int height_, width_;
};
Now, let's say I am using this struct
in some source file.
I don't understand why can't I just directly do
MapMetaData::cell_size_ = my_value
instead of
float MapMetaData::cell_size_ = 0.0; // intializing the static member
MapMetaData::cell_size_ = my_value
- Why do I need to explicily initialize the static member?
- While initializing, why do I need to specify the 'float' specifier?
- Why do I need to do the initialization globally? Why can't I just initalize it inside the scope of some function and then use it inside some another function because it is a static member after all?
Trying to call MapMetaData::cell_size_ = my_value
directly without initializing, I get
undefined reference to MapMetaData::origin_x_
error.
英文:
I am quite confused regarding intialization and usage of C++ class/struct static members.
Let's say, that I have defined a struct MapMetaData
in a header file named Constants.h
. Here's how it looks -
struct MapMetaData {
static float cell_size_, origin_x_, origin_y_;
static unsigned int height_, width_;
};
Now, let's say I am using this struct
in some source file.
I don't understand why can't I just directly do
MapMetaData::cell_size_ = my_value
instead of
float MapMetaData::cell_size = 0.0; // intializing the static member
? // then assigning some value to it
MapMetaData::cell_size_ = my_value
- Why do I need to explicily initialize the static member?
- While initializing, why do I need to specify the 'float' specifier?
- Why do I need to do the initialization globally? Why can't I just initalize it inside the scope of some function and then use it inside some another function because it is a static member after all?
Trying to call MapMetaData::cell_size_ = my_value
directly without initializing, I get
undefined reference to MapMetaData::origin_x_
error.
答案1
得分: 2
在Constants.h
中的声明告诉编译器将会有一个静态变量MapMetaData::cell_size
。这为变量命名,包括Constants.h
的模块可以引用该变量。
稍后,通常会在Constants.cpp
中,变量被定义:
float MapMetaData::cell_size = 0.0f; // 显式初始化
这告诉编译器在程序中分配一块内存来存储该变量。初始化 (... = 0.0f;
) 告诉编译器在程序启动时将哪个值放入该内存块中。静态变量始终会被初始化。如果没有提供值,即:
float MapMetaData::cell_size; // 隐式初始化
那么变量将被初始化为"0"(这对于非静态变量未必成立)。
如果省略了变量定义,则不会创建变量的存储空间,因此链接器无法创建程序,因为它无法解析对该变量的引用。因此,会出现类似你观察到的错误消息。
稍后,您可以分配一个值给变量:
MapMetaData::cell_size = my_value;
这告诉编译器创建代码,将my_value
的内容复制到 MapMetaData::cell_size
中。
因此,这些代码行不是多余的。每个都有特定的功能。
英文:
The declaration in Constants.h
tells the compiler that there will be a static variable MapMetaData::cell_size
. This gives the variable a name and modules including Constants.h
can then refer to that variable.
Later, that would usually be in Constants.cpp
, the variable is defined:
float MapMetaData::cell_size = 0.0f; // Explicit initialization
which tells the compiler to allocate a piece of memory in the program to store the variable. The initialization (... = 0.0f;
) tells the compiler which value to place in that piece of memory when the program starts. Static variables are always initialized. If no value is provided, i.e.:
float MapMetaData::cell_size; // Implicit initialization
then the variable is initialized to "0" (this is not necessarily the case for non-static variables).
If the variable definition is omitted, then the storage for the variable will not be created and thus the linker cannot create the program because it cannot resolve the references to the variable. Thus an error message is given like the one you observed.
Later, you may assign a value to the variable:
MapMetaData::cell_size = my_value;
this tells the compiler to create code which copies the contents of my_value
to MapMetaData::cell_size
.
Thus, these lines of code are not redundant. Each have a specific function.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论