Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
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 2:04
1  
@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 at 10:05
add comment

5 Answers

up vote 29 down vote accepted

It is. test.hpp:

  1 class A {
  2 public:
  3     static int a(int i);
  4 };

test.cpp:

  1 #include <iostream>
  2 #include "test.hpp"
  3 
  4 
  5 int A::a(int i) {
  6     return i + 2;
  7 }
  8 
  9 using namespace std;
 10 int main() {
 11     cout << A::a(4) << endl;
 12 }

They're not always inline, no, but the compiler can make them.

share|improve this answer
add comment

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;
}
share|improve this answer
add comment

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?

share|improve this answer
    
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 2:11
add comment

Sure You can. I'd say that You should.

This article may be usefull:
http://www.learncpp.com/cpp-tutorial/812-static-member-functions/

share|improve this answer
add comment

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" 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?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.