Home > Software engineering >  MFC CWinApp class is how to call in the derived class to rewrite the virtual function InitInstance (
MFC CWinApp class is how to call in the derived class to rewrite the virtual function InitInstance (

Time:09-23

We all know that the MFC initialization process: the InitInstance CWinApp is entry function, usually we write a derived class inherits his, then rewrite the InitInstance function,
Define a global object again, but I didn't understand CWinApp class, how did he call me InitInstance functions in a derived class,

Very confused, the great spirit show

It is better for a simple example explain process,

CodePudding user response:

__tmainCRTStartup first call _initterm to invoke the global object constructor, then the CWinApp: : CWinApp would address to save themselves into a global pointer (so you can't have two global CWinApp object)
Then call the CRT WinMain, this is called AfxWinMain, it will call the global pointer, as for your InitInstance call because InitInstance is declared as virtual functions,

CodePudding user response:

reference 1/f, jiangsheng response:
__tmainCRTStartup first call _initterm to invoke the global object constructor, then the CWinApp: : CWinApp would address to save themselves into a global pointer (so you can't have two global CWinApp object)
Then call the CRT WinMain, this is called AfxWinMain, it will call the global pointer, as for your InitInstance call because InitInstance is declared as virtual functions,


#include
Class CMyApp: public CWinApp
{
Virtual BOOL InitInstance ()
{

return TRUE;
}
};
CMyApp theApp;

Just because statement becomes a virtual function, can call CWinApp derived class to realize virtual function?

CodePudding user response:

I look at file, he is not a pure virtual function

CodePudding user response:

With a base class pointer call virtual functions of derived class this function is called polymorphism, if you learn c + +, the teacher must speak polymorphism,

CWinApp derived from CWinThread, CWinThread: : InitInstance is a virtual function, whether in your derived class and virtual doesn't matter, as long as the signature of the function and the base class virtual function in exactly the same, automatic override the base class virtual function, unless there is class when override the virtual function to add the final keyword banned the derived class to override this function,

CodePudding user response:

reference 4 floor jiangsheng response:
with a base class pointer call virtual functions of derived class this function is called polymorphism, if you learn c + +, the teacher must speak polymorphism,


Pointer to a base class, need a derived class pointer instantiated, to call virtual functions of derived class?
Derived classes of the instantiation of the pointer address come from?

CodePudding user response:

Set a breakpoint in the function, F5 debugging start, stop the breakpoint view the call stack layers after relationship analysis

CodePudding user response:


Just a few steps

CodePudding user response:

Instances of virtual function table has its own virtual functions,

CodePudding user response:

The base class virtual function table has its own virtual function
1 if a derived class does not override the base class virtual function, then the derived class virtual function table to keep the base class virtual function,
2 if a derived class reloading the base class virtual function, then the derived class virtual function table has its own virtual functions,
3 derived class call virtual functions, it is called the virtual function in the virtual function table,

CodePudding user response:

references 9 f schlafenhamster response:
the base class virtual function table has its own virtual function
1 if a derived class does not override the base class virtual function, then the derived class virtual function table to keep the base class virtual function,
2 if a derived class reloading the base class virtual function, then the derived class virtual function table has its own virtual functions,
3 derived class call virtual functions, that is called the virtual function in the virtual function table,


Daniel, can give a simple example, don't explain

CodePudding user response:

A search for "virtual function table"

CodePudding user response:

Brush, make a breakpoint to see to know, to ask people,

CodePudding user response:


Engineering of CPP global variable
 CTestApp theApp; 


Appmodule. CPP
 extern "C" int WINAPI 
_tWinMain (HINSTANCE HINSTANCE, HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine, int nCmdShow)
# pragma warning (4985) suppress:
{
//call Shared/exported WinMain
Return AfxWinMain (hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}



Winmain. CPP, pay attention to the base class CWinApp is CWinThread
 int AFXAPI AfxWinMain (HINSTANCE HINSTANCE, HINSTANCE hPrevInstance, 
_In_ LPTSTR lpCmdLine, int nCmdShow)
{
ASSERT (hPrevInstance==NULL);

Int nReturnCode=1;
CWinThread * pThread=AfxGetThread ();
CWinApp * pApp=AfxGetApp ();

//AFX internal initialization
if (! AfxWinInit (hInstance, hPrevInstance, lpCmdLine, nCmdShow))
Goto InitFailure;

//App global initializations (rare)
If (pApp!=NULL & amp; & ! PApp - & gt; InitApplication ())
Goto InitFailure;

//Perform specific initializations
if (! PThread - & gt; InitInstance ())
{
If (pThread - & gt; M_pMainWnd!=NULL)
{
TRACE (traceAppMsg, 0, "Warning: Destroying non - NULL m_pMainWnd \ n");
PThread - & gt; M_pMainWnd - & gt; DestroyWindow ();
}
NReturnCode=pThread - & gt; ExitInstance ();
Goto InitFailure;
}
NReturnCode=pThread - & gt; The Run ();

InitFailure:
# ifdef _DEBUG
//Check for missing AfxLockTempMap calls
If (AfxGetModuleThreadState () - & gt; M_nTempMapLock!=0)
{
TRACE (traceAppMsg, 0, "Warning: Temp map lock count non - zero % (ld). \ n",
AfxGetModuleThreadState () - & gt; M_nTempMapLock);
}
AfxLockTempMaps ();
AfxUnlockTempMaps (1);
# endif

AfxWinTerm ();
Return nReturnCode;
}



The instance is a virtual function, so calls a derived class instance
 BOOL CTestApp: : InitInstance () 
{
//if a run on Windows XP applications listing specified to
//use ComCtl32. DLL version 6 or higher to enable visual way,
//need InitCommonControlsEx (), otherwise, will not be able to create a window,
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls. DwSize=sizeof (InitCtrls);
//set it to include all want to use in your application of
//the control class,
InitCtrls. DwICC=ICC_WIN95_CLASSES;
InitCommonControlsEx (& amp; InitCtrls);

CWinApp: : InitInstance ();


nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related