C++ 如何在 *.cpp 文件中实现静态类成员函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5373107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement static class member functions in *.cpp file?
提问by CromTheDestroyer
Is it possible to implement static
class member functions in *.cpp file instead of doing
it in the header file ?
是否可以static
在 *.cpp 文件中实现类成员函数而不是在头文件中实现?
Are all static
functions always inline
?
所有的static
功能总是inline
吗?
回答by CromTheDestroyer
It is.
这是。
test.hpp:
测试.hpp:
class A {
public:
static int a(int i);
};
test.cpp:
测试.cpp:
#include <iostream>
#include "test.hpp"
int A::a(int i) {
return i + 2;
}
using namespace std;
int main() {
cout << A::a(4) << endl;
}
They're not always inline, no, but the compiler can make them.
它们并不总是内联的,不,但编译器可以制作它们。
回答by paulcam
Try this:
尝试这个:
header.hxx:
标题.hxx:
class CFoo
{
public:
static bool IsThisThingOn();
};
class.cxx:
类.cxx:
#include "header.hxx"
bool CFoo::IsThisThingOn() // note: no static keyword here
{
return true;
}
回答by Rizz Rocks
helper.hxx
helper.hxx
class helper
{
public:
static void fn1 ()
{ /* defined in header itself */ }
/* fn2 defined in src file helper.cxx */
static void fn2();
};
helper.cxx
helper.cxx
#include "helper.hxx"
void helper::fn2()
{
/* fn2 defined in helper.cxx */
/* do something */
}
A.cxx
A.cxx
#include "helper.hxx"
A::foo() {
helper::fn1();
helper::fn2();
}
To know more about how c++ handles static functions visit: Are static member functions in c++ copied in multiple translation units?
要了解有关 C++ 如何处理静态函数的更多信息,请访问:C++ 中的静态成员函数是否在多个翻译单元中复制?
回答by Mr. Suryaa Jha
In your header file say foo.h
在你的头文件中说foo.h
class Foo{
public:
static void someFunction(params..);
// other stuff
}
In your implementation file say foo.cpp
在你的实现文件中说foo.cpp
#include "foo.h"
void Foo::someFunction(params..){
// Implementation of someFunction
}
Very Important
很重要
Just make sure you don't use the static keyword in your method signature when you are implementing the static function in your implementation file.
在实现文件中实现静态函数时,请确保不要在方法签名中使用 static 关键字。
Good Luck
祝你好运
回答by cppcoder
Yes you can define static member functions in *.cpp file. If you define it in the header, compiler will by default treat it as inline. However, it does not mean separate copies of the static member function will exist in the executable. Please follow this post to learn more about this: Are static member functions in c++ copied in multiple translation units?
是的,您可以在 *.cpp 文件中定义静态成员函数。如果在头文件中定义它,编译器默认将其视为内联。但是,这并不意味着静态成员函数的单独副本将存在于可执行文件中。请关注这篇文章以了解更多相关信息: C++ 中的静态成员函数是否在多个翻译单元中复制?
回答by Don Mclachlan
@crobar, you are right that there is a dearth of multi-file examples, so I decided to share the following in the hopes that it helps others:
@crobar,您说得对,缺少多文件示例,因此我决定分享以下内容,希望对他人有所帮助:
::::::::::::::
main.cpp
::::::::::::::
#include <iostream>
#include "UseSomething.h"
#include "Something.h"
int main()
{
UseSomething y;
std::cout << y.getValue() << '\n';
}
::::::::::::::
Something.h
::::::::::::::
#ifndef SOMETHING_H_
#define SOMETHING_H_
class Something
{
private:
static int s_value;
public:
static int getValue() { return s_value; } // static member function
};
#endif
::::::::::::::
Something.cpp
::::::::::::::
#include "Something.h"
int Something::s_value = 1; // initializer
::::::::::::::
UseSomething.h
::::::::::::::
#ifndef USESOMETHING_H_
#define USESOMETHING_H_
class UseSomething
{
public:
int getValue();
};
#endif
::::::::::::::
UseSomething.cpp
::::::::::::::
#include "UseSomething.h"
#include "Something.h"
int UseSomething::getValue()
{
return(Something::getValue());
}
回答by Blair Houghton
The #include
directive literally means "copy all the data in that file to this spot." So when you include the header file, it's textually within the code file, and everything in it will be there, give or take the effect of other directives or macro replacements, when the code file (now called the compilation unitor translation unit) is handed off from the preprocessor module to the compiler module.
该#include
指令的字面意思是“将该文件中的所有数据复制到该位置”。因此,当您包含头文件时,它以文本形式存在于代码文件中,并且当代码文件(现在称为编译单元或翻译单元)处于从预处理器模块移交给编译器模块。
Which means the declaration and definition of your static member function were really in the same file all along...
这意味着您的静态成员函数的声明和定义实际上一直在同一个文件中......
回答by Sebastian Dusza
Sure You can. I'd say that You should.
你当然可以。我会说你应该。
This article may be usefull:
http://www.learncpp.com/cpp-tutorial/812-static-member-functions/
这篇文章可能有用:http:
//www.learncpp.com/cpp-tutorial/812-static-member-functions/