C++ 哪个函数用于初始化静态类成员?
我有一个关于选择哪个函数初始化静态类成员的问题C++ 哪个函数用于初始化静态类成员?,c++,static-members,argument-dependent-lookup,C++,Static Members,Argument Dependent Lookup,我有一个关于选择哪个函数初始化静态类成员的问题 //Base.h class Base { private: static int count; static int countInit() { return 10; } public: Base() { } }; //and Base.cpp static int countInit() { return 0; } int Base::count=countIn
//Base.h
class Base
{
private:
static int count;
static int countInit()
{
return 10;
}
public:
Base()
{
}
};
//and Base.cpp
static int countInit()
{
return 0;
}
int Base::count=countInit();//member function is used.
static int local_count=countInit();//the local one in Base.cpp
变量
Base::count
是用Base::countInit()
初始化的,而不是Base.cpp中定义的countInit()
。但是local\u count
是由localcountInit
初始化的。所以,我想知道,在这种情况下是否有类似于Koenig lookup的规则?在您编写int Base::count
之后,您就在类Base
中,所以将调用类的静态函数。此处将使用非限定查找
从3.4.2/13开始
定义类X(9.4.2)的静态数据成员时使用的名称(在静态数据的限定id之后) 查找成员)时,就好像在X的成员函数中使用了该名称一样。
从9.4.2开始静态数据成员的定义应出现在名称空间中 包含成员的类定义的范围。在命名空间范围的定义中,静态 数据成员应使用::运算符通过其类名进行限定中的初始值设定项表达式 静态数据成员的定义在其类的范围内
例如:class process {
static process* run_chain;
static process* running;
};
process* process::running = get_main();
process* process::run_chain = running;
So
int Base::count=countInit()代码>呼叫成员?@LuchianGrigore,是的,是这样。我不知道发生了什么。调用“int Base::count=countInit();”的位置在哪里?导致静态成员函数Base::countInit()count不能仅由类Base作用域外的countInit()调用。Try::countInit(),这会有区别吗?@NeilKirk如果调用::countInit(),则使用本地countInit。