Question

Code

 static void MyClass::ThreadEntryStatic()
 {
    //...
 }

 void MyClass::Begin()
 {
         CreateThread(..,ThreadEntryStatic,..);
 }

In which condition we should use the static in class ?

Was it helpful?

Solution

There are three places that the keyword static can be used. One is in the declaration of a struct/class when that context means that the method is a class method instead of an instance method. This means that this method is called directly and you don't need an instance. From this static method, you can not access instance variables.

In MyClass.h

struct MyClass
{
  static void ThreadEntryStatic();

  void Begin();
};

In MyClass.cpp

void MyClass::ThreadEntryStatic()
{
}

void MyClass::Begin()
{
  CreateThread(.., MyClass::ThreadEntryStatic, ...);
}

A second case where the static keyword is use is in the scope of a file where you don't want the visibility of the variable declared to be visible outside the file. You can also use an anonymous namespace for this as well.

A third case where the static keyword is used is in the scope of a method and the value is retained between executions of the function (and initialized with the assignment the first time).

OTHER TIPS

If you are running static methods on multiple threads you need to be paying very close attention to synchronizing your code. In my opinion, when doing multithreaded programming, I try to use a separate instance of my object or work item per thread, and avoid any type of static or shared data at all. Of course this is not always possible, so threading remains one of the trickiest programming areas to deal with.

As a concrete example,

class Test{
      static void foo();      
};

static void Test::foo(){
    // code here
}

is not going to compile, you are not able to declare a function with static keyword outside of the class declaration. You simply have to remove static keyword when you are implementing the function.

class Test{
      static void foo();      
};

void Test::foo(){
    // code here
}

A few people have touched on this, but static used for internal linkage should not be used, instead one should use an anonymous namespace:

namespace
{

void myInternallyLinkedFunction()
{
    // do something
}

int myInternallyLinkedInteger;

class myInternallyLinkedClass
{
public:
    void doSomething();
};

} // anon namespace


void myExternallyLinkedFunction()
{

    ++myInternallyLinkedInteger;
    myInternallyLinkedFunction();
    myInternallyLinkedClass x;
    x.doSomething();
}

The value of static variables are retained between function calls. Check this MSDN entry for examples. Defining and using "static" methods has been outlined in chrish's answer

Static could be used when implementing singleton classes where you need to have just one instance of the class. It's usage depends on the context.

What your example shows is a "static member function thread callback" pattern. As the thread function must have a WINAPI signature, it cannot be a normal member function, only a static member. Often you pass this as a thread parametr to this callback, and then call a real member performing the thread work.

There is just one use of a static member, and there are many different. It is really hard to guess what the purpose of your question is. Are you solving some particular problem, or are you just interested about all possible uses of static members or static member functions?

More complete example of "static member function thread callback":

class MyClass
{

  /// background rendering thread
  int ThreadEntry()
  {
     // do the work here
  }

  /// static member callback "proxy"
  static DWORD WINAPI ThreadEntryStatic(void *param)
  {
    return ((EngineDD9 *)param)->ThreadEntry();
  }

  void SpawnThread()
  {
    CreateThread(.., ThreadEntryStatic, ...);
  }

};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top