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

C++

C++ Problem Overview


Is it possible to implement static class member functions in *.cpp file instead of doing it in the header file ?

Are all static functions always inline ?

C++ Solutions


Solution 1 - C++

It is. The key is to use the static keyword only in the header file, not in the source file!

test.hpp:

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

test.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.

Solution 2 - C++

Try this:

header.hxx:

class CFoo
{
public: 
    static bool IsThisThingOn();
};

class.cxx:

#include "header.hxx"
bool CFoo::IsThisThingOn() // note: no static keyword here
{
    return true;
}

Solution 3 - C++

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: https://stackoverflow.com/questions/5372091/are-static-member-functions-in-c-copied-in-multiple-translation-units

Solution 4 - C++

In your header file say foo.h

class Foo{
    public:
        static void someFunction(params..);
    // other stuff
}

In your implementation file say 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.

Good Luck

Solution 5 - C++

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: https://stackoverflow.com/questions/5372091/are-static-member-functions-in-c-copied-in-multiple-translation-units

Solution 6 - 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:

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

Solution 7 - C++

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 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...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser660975View Question on Stackoverflow
Solution 1 - C++CromTheDestroyerView Answer on Stackoverflow
Solution 2 - C++paulcamView Answer on Stackoverflow
Solution 3 - C++Rizz RocksView Answer on Stackoverflow
Solution 4 - C++Mr. Suryaa JhaView Answer on Stackoverflow
Solution 5 - C++cppcoderView Answer on Stackoverflow
Solution 6 - C++Don MclachlanView Answer on Stackoverflow
Solution 7 - C++Blair HoughtonView Answer on Stackoverflow