문제

암호

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

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

어떤 조건에서 우리는 수업 시간에 정적을 사용해야합니까?

도움이 되었습니까?

해결책

키워드 정적을 사용할 수있는 세 곳이 있습니다. 하나는 해당 컨텍스트가 메소드가 인스턴스 메소드 대신 클래스 메소드임을 의미 할 때 구조/클래스 선언에 있습니다. 이것은이 방법이 직접 호출되며 인스턴스가 필요하지 않음을 의미합니다. 이 정적 메소드에서 인스턴스 변수에 액세스 할 수 없습니다.

myclass.h에서

struct MyClass
{
  static void ThreadEntryStatic();

  void Begin();
};

myclass.cpp

void MyClass::ThreadEntryStatic()
{
}

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

정적 키워드가 사용되는 두 번째 사례는 파일 외부에서 표시되는 변수의 가시성을 원하지 않는 파일의 범위에 있습니다. 이를 위해 익명 네임 스페이스를 사용할 수도 있습니다.

정적 키워드가 사용되는 세 번째 사례는 메소드의 범위에 있으며 함수 실행 사이에 값이 유지되고 (처음에는 할당으로 초기화) 값이 유지됩니다.

다른 팁

여러 스레드에서 정적 메소드를 실행하는 경우 코드 동기화에 매우주의를 기울여야합니다. 제 생각에는 멀티 스레드 프로그래밍을 할 때 스레드 당 객체 또는 작업 항목의 별도의 인스턴스를 사용하고 모든 유형의 정적 또는 공유 데이터를 피하려고합니다. 물론 이것은 항상 가능한 것은 아니므로 스레딩은 가장 까다로운 프로그래밍 영역 중 하나입니다.

구체적인 예로

class Test{
      static void foo();      
};

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

컴파일하지 않으면 클래스 선언 외부에서 정적 키워드가있는 함수를 선언 할 수 없습니다. 함수를 구현할 때는 정적 키워드를 제거하면됩니다.

class Test{
      static void foo();      
};

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

몇몇 사람들이 이것에 대해 다루었지만 static 내부 링키지에 사용되는 데 사용되어서는 안됩니다. 대신 익명 네임 스페이스를 사용해야합니다.

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, ...);
  }

};
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top