출처 : 윈도우 프로그래머를 위한 MFC 구조와 원리
 
#include <stdio.h>
#include <assert.h>
 
#define AfxGetApp()  KSingleton<CWinApp>::GetSingleton()
 
template < typename T >
class KSingleton
{
private:
  static T* ms_pSingleton;
 
public:
  KSingleton()
  {
    assert( NULL == KSingleton<T>::ms_pSingleton );
    KSingleton<T>::ms_pSingleton = (T*)this;
  }
 
  ~KSingleton()
  {
    KSingleton<T>::ms_pSingleton = NULL;
  }
 
  static T* GetSingleton()
  {
    return ms_pSingleton;
  }
}; // class KSingleton
 
 
/*static*/ template < typename T > T* KSingleton<T>::ms_pSingleton = NULL;

class CWinApp : public KSingleton<CWinApp>
{
public:
  void Print()
  {
    printf( "Hello\n" );
  }
}; // class CWinApp
 
 
CWinApp theApp;
 
 
int main(int argc, char** argv)
{
  // KSingleton<CWinApp>::GetSingleton()->Print();
  AfxGetApp()->Print();
 
  return 0;
}
 
2006/10/19 11:15 2006/10/19 11:15

글 걸기 주소 : 이 글에는 트랙백을 보낼 수 없습니다

덧글을 달아 주세요