출처 : 윈도우 프로그래머를 위한 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;
}
#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;
}
덧글을 달아 주세요