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 开发者_StackOverflowshould i do? for that.
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()
{
}
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
精彩评论