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?
It doesn't implement classis singleton pattern. Try to create second CWinApp-derived object - the program is compiled, but gives assertion when executed:
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.
Last edited by Alex F; July 22nd, 2010 at 12:41 AM.
It doesn't implement classis singleton pattern. Try to create second CWinApp-derived object - the program is compiled, but gives assertion when executed:
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?
Last edited by zxjun84; July 22nd, 2010 at 07:35 AM.
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.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Bookmarks