是否可以在*.cpp文件中实现static
类成员函数,而不是在头文件中实现?
所有的static
函数都是inline
吗
发布于 2011-03-21 10:04:26
它是。
test.hpp:
class A {
public:
static int a(int i);
};
test.cpp:
#include <iostream>
#include "test.hpp"
int A::a(int i) {
return i + 2;
}
using namespace std;
int main() {
cout << A::a(4) << endl;
}
它们并不总是内联的,不是,但编译器可以生成它们。
发布于 2011-03-21 10:04:13
试试这个:
header.hxx:
class CFoo
{
public:
static bool IsThisThingOn();
};
class.cxx:
#include "header.hxx"
bool CFoo::IsThisThingOn() // note: no static keyword here
{
return true;
}
发布于 2013-08-02 12:55:12
helper.hxx
class helper
{
public:
static void fn1 ()
{ /* defined in header itself */ }
/* fn2 defined in src file helper.cxx */
static void fn2();
};
helper.cxx
#include "helper.hxx"
void helper::fn2()
{
/* fn2 defined in helper.cxx */
/* do something */
}
A.cxx
#include "helper.hxx"
A::foo() {
helper::fn1();
helper::fn2();
}
要了解c++如何处理静态函数的更多信息,请访问:Are static member functions in c++ copied in multiple translation units?
https://stackoverflow.com/questions/5373107
复制相似问题