is it possible to define the static member function of a class in .cpp file instead of its header file?

霸气de小男生 提交于 2019-12-25 17:05:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!