Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 does MFC realizes the singleton pattern for CWinApp?
Posted

MFC does not implement CWinApp as a singleton, simply each application instantiate just one object of that class, but if you write something like this in your code:

C++
CWinApp theApp1;
CWinApp theApp2;


You obtain two different instances of CWinApp
 
Share this answer
 
as Sauro Viti said CWinApp is not a singleton class.
while declaring a object of CWinApp class MFC stores first object to afxCurrentWinApp veriable.
Remember that MFC project has also WinMain(...) function.

We can see what will happen when we declare an object CWinApp theApp1;

1) It will call constructor of CWinApp [Because it is global variable]
2) Inside constructor afxCurrentWinApp object will set to current object ( this)
3) after all global variables construction WinMain will invoke.
4) inside WinMain it will call the CWinApp's InitInstance() [ because it is vertual function]

Note: When we call AfxGetApp() we will return the afxCurrentWinApp.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900