To create a static member function we need to use the static keyword while declaring the function. Since static member variables are class properties and not object properties, to access them we need to use the class name instead of the object name.
We are allowed to invoke a static member function using the object and the '. ' operator but it is recommended to invoke the static members using the class name and the scope resolution operator.
Static Keyword in C++Static functions can be called directly by using class name. Static variables are initialized only once. Compiler persist the variable till the end of the program. Static variable can be defined inside or outside the function.
For the static variables, we have to initialize them after defining the class. To initialize we have to use the class name then scope resolution operator (::), then the variable name. Now we can assign some value.
It is.
test.hpp:
class A {
public:
static int a(int i);
};
test.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.
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;
}
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?
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
}
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
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?
@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());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With