我看孙鑫老师的VC视频讲到基类CWinApp的构造函数中有个this指针归属问题,孙鑫老师讲到该this指针由多态性原理属于子类CmyApp指针,有点不懂?看下面代码:
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();
AFX_MODULE_THREAD_STATE* pThreadState = pModuleState->m_thread;
ASSERT(AfxGetThread() == NULL);
pThreadState->m_pCurrentWinThread = this; //该this指针为什么是子类CmyApp指针;
ASSERT(AfxGetThread() == this);
m_hThread = ::GetCurrentThread();
m_nThreadID = ::GetCurrentThreadId(); // initialize CWinApp state
ASSERT(afxCurrentWinApp == NULL); // only one CWinApp object please
pModuleState->m_pCurrentWinApp = this;
ASSERT(AfxGetApp() == this); // in non-running state until WinMain
m_hInstance = NULL;
m_pszHelpFilePath = NULL;
m_pszProfileName = NULL;
m_pszRegistryKey = NULL;
m_pszExeName = NULL;
m_pRecentFileList = NULL;
m_pDocManager = NULL;
m_atomApp = m_atomSystemTopic = NULL;
m_lpCmdLine = NULL;
m_pCmdInfo = NULL; // initialize wait cursor state
m_nWaitCursorCount = 0;
m_hcurWaitCursorRestore = NULL; // initialize current printer state
m_hDevMode = NULL;
m_hDevNames = NULL;
m_nNumPreviewPages = 0; // not specified (defaults to 1) // initialize DAO state
m_lpfnDaoTerm = NULL; // will be set if AfxDaoInit called // other initialization
m_bHelpMode = FALSE;
m_nSafetyPoolSize = 512; // default size
}就是pThreadState->m_pCurrentWinThread = this; 该this指针为什么是子类CmyApp指针,望各位大侠帮忙小弟理解,TKS!!
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();
AFX_MODULE_THREAD_STATE* pThreadState = pModuleState->m_thread;
ASSERT(AfxGetThread() == NULL);
pThreadState->m_pCurrentWinThread = this; //该this指针为什么是子类CmyApp指针;
ASSERT(AfxGetThread() == this);
m_hThread = ::GetCurrentThread();
m_nThreadID = ::GetCurrentThreadId(); // initialize CWinApp state
ASSERT(afxCurrentWinApp == NULL); // only one CWinApp object please
pModuleState->m_pCurrentWinApp = this;
ASSERT(AfxGetApp() == this); // in non-running state until WinMain
m_hInstance = NULL;
m_pszHelpFilePath = NULL;
m_pszProfileName = NULL;
m_pszRegistryKey = NULL;
m_pszExeName = NULL;
m_pRecentFileList = NULL;
m_pDocManager = NULL;
m_atomApp = m_atomSystemTopic = NULL;
m_lpCmdLine = NULL;
m_pCmdInfo = NULL; // initialize wait cursor state
m_nWaitCursorCount = 0;
m_hcurWaitCursorRestore = NULL; // initialize current printer state
m_hDevMode = NULL;
m_hDevNames = NULL;
m_nNumPreviewPages = 0; // not specified (defaults to 1) // initialize DAO state
m_lpfnDaoTerm = NULL; // will be set if AfxDaoInit called // other initialization
m_bHelpMode = FALSE;
m_nSafetyPoolSize = 512; // default size
}就是pThreadState->m_pCurrentWinThread = this; 该this指针为什么是子类CmyApp指针,望各位大侠帮忙小弟理解,TKS!!
wltg2001 好呀,我是这么理解的,一般类的成员函数参数里都是隐含一个this指针;但this是在CWinApp类的构造函数里使用,this应该为父类CWinApp指针呀
wltg2001大侠可不可以帮俺讲讲呀,我有点搞不明白,TKS!!
hztj2005 好呀,TKS你的发言,你的意思是不是可以这样理解,如下:
(1)当定义CmyWin theApp时 CmyWin是子类对象先调用父类CWinApp构造函数然后在调用子类CmyApp构 造函数,在父类CWinApp构造函数里this是子类CmyApp指针;
(2)当定义CWinApp theApp时,在父类CWinApp构造函数里this是父类CWinApp指针有没有这方面的测试小程序,如果测试小程序,这样更容易理解点
wltg2001大侠可不可以帮俺讲讲呀,我有点搞不明白,TKS!!
==============================
this顾名思义,它在任何情况下都是指向对象自身的。
=====================================
对象是类的具体的实例,它是具体的,类是抽象的;this是指向对象的,但是对象具体属于是哪个类的类型,就好像我们定义一个变量,int a;也要指明变量a的类型是整型一样。
=============
对啊,对象是属于类型的,所以你定义这个对象是什么类型的,它就是什么类型的啊!你上面的代码中的对象是Cmyapp类的,那么它就是Cmyapp类的啊,总不能因为子类调用父类的构造函数,这个对象就成了父类吧?
================
TKS wltg2001的发言,现在我们回到问题的起始点,我的问题是CWinApp类的构造函数里哪个this指针的类型归属问题,它是属于基类CWinApp类还是子类CmyApp,按照孙鑫老师的视频里说由多态性原理属于属于子类CmyApp类型;
经过我多方研究与分析得到你的全局对象theApp定义成子类CmyApp,this就属于子类CmyApp;全局对象theApp定义成基类CWinApp类,this就属于基类CWinApp类;
毕竟全局对象是MFC程序的起始点;
=================================================
在MFC程序中全局变量有定义成CWinApp类的吗?应该都是定义成它的子类的吧。子类对象在执行构造函数时会先调用父类的构造函数,但是,此时这个对象并没有变成CWinApp类,它还是一个子类对象。
{
public:
Animal()
{
this->Eat();
}
virtual void Eat()
{
cout<<"Animal eat..."<<endl;
}
virtual ~Animal(){}
};class Fish :public Animal
{
public:
Fish()
{}
void Eat()
{
cout<<"Animal eat..."<<endl;
}
virtual ~Fish()
{
}
};void main()
{
Fish fish;
}
{
Fish fish;
}修改成
Fish fish;
void main()
{}; 经过调试发现 this果然是fish势力,但是执行的eat方法都是animal中的,animal eat设置成virtual都没用