简体   繁体   English

如何在 *.cpp 文件中实现静态类成员函数?

[英]How to implement static class member functions in *.cpp file?

Is it possible to implement static class member functions in *.cpp file instead of doing it in the header file ?是否可以在 *.cpp 文件中实现static类成员函数而不是在头文件中实现?

Are all static functions always inline ?所有static函数都总是inline吗?

It is.这是。 The key is to use the static keyword only in the header file, not in the source file!关键是只在文件中使用static关键字,而不是在源文件中!

test.hpp:测试.hpp:

class A {
public:
    static int a(int i);  // use `static` here
};

test.cpp:测试.cpp:

#include <iostream>
#include "test.hpp"


int A::a(int i) {  // do **not** use `static` here!
    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.它们并不总是内联的,不,但编译器可以制作它们。

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;
}

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();
}

To know more about how c++ handles static functions visit: Are static member functions in c++ copied in multiple translation units?要了解有关 c++ 如何处理静态函数的更多信息,请访问: C++ 中的静态成员函数是否在多个翻译单元中复制?

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祝你好运

Yes you can define static member functions in *.cpp file.是的,您可以在 *.cpp 文件中定义静态成员函数。 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?请关注这篇文章以了解更多信息: C++ 中的静态成员函数是否在多个翻译单元中复制?

@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());
}

The #include directive literally means "copy all the data in that file to this spot." #include指令的字面意思是“将该文件中的所有数据复制到该位置”。 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 unit or translation unit ) is handed off from the preprocessor module to the compiler module.因此,当您包含头文件时,它以文本形式存在于代码文件中,并且其中的所有内容都将存在,当代码文件(现在称为编译单元翻译单元)是从预处理器模块移交给编译器模块。

Which means the declaration and definition of your static member function were really in the same file all along...这意味着您的静态成员函数的声明和定义一直在同一个文件中......

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/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 static 库中实现 Class 的部分成员函数,而在 cpp 文件中实现 rest 函数? - How to implement parts of member functions of a Class in a static library while the rest functions implemented in a cpp file? 在.cpp文件中使用类的静态成员 - using static member of a class in .cpp file 在cpp文件中实现非模板类的模板成员 - Implement template member of non-template class in cpp file 在 .cpp 文件中定义静态类成员时的未定义引用错误 - undefined reference error for static class member when it is defined in .cpp file 如何通过仅在cpp文件中初始化静态成员的情况 - how to initialize a static member in cpp file only situation via a 如何使用匿名名称空间在.cpp文件中设置静态成员变量? - How to set a static member variable in the .cpp file using anonymous namespace? 如何在cpp文件中调用静态成员函数 - how to call a static member function inside a cpp file 如何访问与主文件不同的文件(.cpp)中的非成员函数 - How to access non member functions that are in a seperate file (.cpp) than main 在 .h 文件中声明时如何使用 .cpp 文件中的模板实现函数 - How to implement functions with template in .cpp file when declared in .h file cpp中的pthread用于类中的不同成员函数 - pthread in cpp for different member functions in a class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM