I have a question about which function is chosen to init a static class member.
//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
The variable Base::count
is initialized with Base::countInit()
rather than the countInit()
defined in Base.cpp. But the local_count
is initialized by the local countInit
. So, I wonder, is there a rule like Koenig lookup within this case?
After you write int Base::count
you are in class Base
, so static function of class will be called. Unqualified lookup will be used here
from 3.4.2/13
A name used in the definition of a static data member of class X (9.4.2) (after the qualified-id of the static member) is looked up as if the name was used in a member function of X.
from 9.4.2
The definition for a static data member shall appear in a namespace scope enclosing the member’s class definition. In the definition at namespace scope, the name of the static data member shall be qualified by its class name using the :: operator. The initializer expression in the definition of a static data member is in the scope of its class
Example:
class process { static process* run_chain; static process* running; }; process* process::running = get_main(); process* process::run_chain = running;