问题
i am having a function which should be run only once for all instance of the class.i thought to use the static function calling method. all the web example shows that static function define in the Header file(inside the class) itself. my function is big one i cant define that in header file what should i do? for that.
回答1:
Like you do for normal functions:
FooBar.h
#ifndef FOOBAR_H
#define FOOBAR_H
class FooBar
{
public:
static void test();
};
#endif
FooBar.cpp
#include "FooBar.h"
void FooBar::test()
{
}
回答2:
If using linux
static pthread_once_t semaphore = PTHREAD_ONCE_INIT;
pthread_once( & semaphore, FooBar::test() );
So you can be sure to go once in your function
来源:https://stackoverflow.com/questions/6911126/is-it-possible-to-define-the-static-member-function-of-a-class-in-cpp-file-inst