【发布时间】:2013-07-17 09:22:20
【问题描述】:
我有一个关于选择哪个函数来初始化静态类成员的问题。
//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_count 是由本地countInit 初始化的。那么,我想知道,在这种情况下是否有像 Koenig 查找 这样的规则?
【问题讨论】:
-
所以
int Base::count=countInit();给会员打电话? -
@LuchianGrigore,是的,确实如此。我不知道发生了这种情况。
-
“int Base::count=countInit();”在哪里被调用?导致静态成员函数 Base::countInit() 计数不能仅由 countInit() 调用,超出类 Base 的范围。
-
试试 ::countInit(),有什么不同吗?
-
@NeilKirk 如果调用 ::countInit(),则使用本地 countInit。
标签: c++ static-members argument-dependent-lookup