是否可以在.cpp文件中定义类的静态成员函数而不是其头文件?

时间:2011-08-02 11:13:03

标签: c++ class constructor static-members

我有一个函数应该只对class.i的所有实例运行一次,以为使用静态函数调用方法。所有Web示例都显示静态函数在Header文件(类内)中定义。我的功能很大我无法在头文件中定义我该怎么办?为此。

3 个答案:

答案 0 :(得分:6)

与正常功能一样:

FooBar.h

#ifndef FOOBAR_H
#define FOOBAR_H


class FooBar
{
public:
    static void test();
};

#endif

FooBar.cpp

#include "FooBar.h"

void FooBar::test()
{

}

答案 1 :(得分:2)

如果使用linux

static pthread_once_t semaphore = PTHREAD_ONCE_INIT;
pthread_once( & semaphore, FooBar::test() );

所以你可以确保在你的功能中使用一次

答案 2 :(得分:0)

如果有人想要了解有关静态检查的更多信息 Link。您将获得所有详细信息。

相关问题