Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 哪个函数用于初始化静态类成员?_C++_Static Members_Argument Dependent Lookup - Fatal编程技术网

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
是由local
countInit
初始化的。所以,我想知道,在这种情况下是否有类似于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。