155

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?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
  • 4
    Could you explain why you "CANNOT" implement the static class memeber function in your cpp file? any error? It is usually no limitation about where you implement such function. – winterTTr Mar 21 '11 at 02:04
  • 12
    @winterTTr, The question probably arose because most examples/tutorials on the web do not present a separate implementation example, instead declaring and defining it in the header. At least the first six hits in my favourite search engine for "C++ static member function" all do it this way and don't explain how you implement it in separate files for a novice. – crobar Jan 31 '14 at 10:05
  • 15
    When you implement, don't repeat the `static` keyword. Write the `static` keyword only in the class definition in the header file – SomethingSomething Jun 25 '14 at 07:07
  • @crobar, you are right that there is a dearth of multi-file examples. I struggled to figure this out, so I decided to share the following: – Don Mclachlan Nov 17 '16 at 16:16

7 Answers7

187

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.

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
CromTheDestroyer
  • 3,616
  • 3
  • 20
  • 26
53

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;
}
paulcam
  • 884
  • 5
  • 14
10

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?

Community
  • 1
  • 1
Rizz Rocks
  • 101
  • 1
  • 3
6

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

Mr. Suryaa Jha
  • 1,516
  • 16
  • 10
2

@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());
}
Don Mclachlan
  • 419
  • 4
  • 8
2

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?

Community
  • 1
  • 1
cppcoder
  • 1,194
  • 4
  • 16
  • 30
  • If you define it in the class body, it will automatically be default. If it's in the header outside the class body, it had better be marked either `inline` or `template` or you'll get multiple definition errors from the linker. – Ben Voigt Mar 21 '11 at 02:11
0

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

Blair Houghton
  • 467
  • 3
  • 10