how CWinApp realize the singleton pattern?
Generally,singleton pattern is applied by the way that the ctor is private and gives a member funtion named getInstance, but CWinApp's instance is defined as a global object,its ctor is not private.So how MFC realizes the singleton pattern for CWinApp?
Re: how CWinApp realize the singleton pattern?
It doesn't implement classis singleton pattern. Try to create second CWinApp-derived object - the program is compiled, but gives assertion when executed:
Code:
CWinApp::CWinApp(LPCTSTR lpszAppName)
{
if (lpszAppName != NULL)
m_pszAppName = _tcsdup(lpszAppName);
else
m_pszAppName = NULL;
// initialize CWinThread state
AFX_MODULE_STATE* pModuleState = _AFX_CMDTARGET_GETSTATE();
ENSURE(pModuleState);
AFX_MODULE_THREAD_STATE* pThreadState = pModuleState->m_thread;
ENSURE(pThreadState);
ASSERT(AfxGetThread() == NULL); // <- this assertion fails
...
}
So, the first CWinApp instance constructor makes some initialization, setting global variables. The second instance asserts that these variables are not initialized. Access to the only CWinApp-derived instance is done using AFX_MODULE_STATE global variable.
Re: how CWinApp realize the singleton pattern?
MFC was around long before design patterns such as the Singleton became mainstream.
Re: how CWinApp realize the singleton pattern?
Quote:
Originally Posted by
Alex F
It doesn't implement classis singleton pattern. Try to create second CWinApp-derived object - the program is compiled, but gives assertion when executed:
Code:
CWinApp::CWinApp(LPCTSTR lpszAppName)
{
if (lpszAppName != NULL)
m_pszAppName = _tcsdup(lpszAppName);
else
m_pszAppName = NULL;
// initialize CWinThread state
AFX_MODULE_STATE* pModuleState = _AFX_CMDTARGET_GETSTATE();
ENSURE(pModuleState);
AFX_MODULE_THREAD_STATE* pThreadState = pModuleState->m_thread;
ENSURE(pThreadState);
ASSERT(AfxGetThread() == NULL); // <- this assertion fails
...
}
So, the first CWinApp instance constructor makes some initialization, setting global variables. The second instance asserts that these variables are not initialized. Access to the only CWinApp-derived instance is done using AFX_MODULE_STATE global variable.
thanks for your reply!
In debug mode, a assert will be throwed.Ignoring the assert,the application would continue to execute well. If creating second CWinApp-derived object in release mode, there is no assert.Does the second instance overwrite the first instance?Is it that CWinApp does not strictly apply singleton pattern?
Re: how CWinApp realize the singleton pattern?
Quote:
Originally Posted by
zxjun84
Strictly speaking, a singleton is a class of which there can be at most one instance. However, sometimes the name is also used for classes of which there should be at most one instance.
Note that even if there can be at most one instance, it still isn't specified how that is enforced. It may be at compile time (preferably), or it may be at run time.
Re: how CWinApp realize the singleton pattern?
I have no idea, what happens in Release mode, or in Debug mode, if assertion is ignored. Undefined behavior or exception. It is not difficult to test.
Re: how CWinApp realize the singleton pattern?
Quote:
Is it that CWinApp does not strictly apply singleton pattern?
As Alex and Arjay said, CWinApp does not implement the singleton pattern. It bears the requirement that you don't create a second instance. Period.