MFC 虚函数

// Test.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "Test.h"
#include "TestDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/
// CTestApp

BEGIN_MESSAGE_MAP(CTestApp, CWinApp)
	//{{AFX_MSG_MAP(CTestApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/
// CTestApp construction

CTestApp::CTestApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/
// The one and only CTestApp object

CTestApp theApp;

/*
1.全局函数(程序入口):
  _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
  {
	  // call shared/exported WinMain
	  return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow); // 全局函数
	}
  看见了吗?里面调用了AfxWinMain(),也就说MFC把对WinMain()的调用转化为对AfxWinMain()的调用,这更加说明,即使在MFC中,
  也没有什么其它神奇的方法能够跳过或换掉对WinMain()的调用。
  
	
2.在WINMAIN.CPP文件中,实现了AfxWinMain()。
	AfxWinMain()才实际调用CWinApp->InitApplication()、CWinApp->->InitInstance()完成系统初始化。
	
int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
	ASSERT(hPrevInstance == NULL);

	int nReturnCode = -1;
	CWinThread* pThread = AfxGetThread(); // 得到进程指针
	CWinApp* pApp = AfxGetApp();  // CWinApp 是 CWinThread 的爸

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

	// App global initializations (rare)
	if (pApp != NULL && !pApp->InitApplication()) // 虚函数 子类实现了用子类的 子类没实现 用父类的
		goto InitFailure;

	// Perform specific initializations
	if (!pThread->InitInstance())            // // 虚函数 子类实现了用子类的 子类没实现 用父类的
	{
		if (pThread->m_pMainWnd != NULL)
		{
			TRACE0("Warning: Destroying non-NULL m_pMainWnd\n");
			pThread->m_pMainWnd->DestroyWindow();
		}
		nReturnCode = pThread->ExitInstance();
		goto InitFailure;
	}
	nReturnCode = pThread->Run();

	InitFailure:
	#ifdef _DEBUG
	// Check for missing AfxLockTempMap calls
	if (AfxGetModuleThreadState()->m_nTempMapLock != 0)
	{
		TRACE1("Warning: Temp map lock count non-zero (%ld).\n",
		AfxGetModuleThreadState()->m_nTempMapLock);
	}
	AfxLockTempMaps();
	AfxUnlockTempMaps(-1);
	#endif

	AfxWinTerm();
	return nReturnCode;
}

*/

//DEL BOOL CTestApp::InitInstance()
//DEL {
//DEL 	// Standard initialization
//DEL 	// If you are not using these features and wish to reduce the size
//DEL 	//  of your final executable, you should remove from the following
//DEL 	//  the specific initialization routines you do not need.
//DEL 
//DEL 	CTestDlg dlg;
//DEL 	m_pMainWnd = &dlg;
//DEL 	int nResponse = dlg.DoModal();
//DEL 	if (nResponse == IDOK)
//DEL 	{
//DEL 		// TODO: Place code here to handle when the dialog is
//DEL 		//  dismissed with OK
//DEL 	}
//DEL 	else if (nResponse == IDCANCEL)
//DEL 	{
//DEL 		// TODO: Place code here to handle when the dialog is
//DEL 		//  dismissed with Cancel
//DEL 	}
//DEL 
//DEL 	// Since the dialog has been closed, return FALSE so that we exit the
//DEL 	//  application, rather than start the application's message pump.
//DEL 	return FALSE;
//DEL }

BOOL CTestApp::InitApplication() 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CWinApp::InitApplication();
}

BOOL CTestApp::InitInstance() 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CWinApp::InitInstance();
}

虚函数用法就是 

1.使用子类定义对象

CTestApp theApp;

2.使用父类指针指向子类对象

 

	CWinThread* pThread = AfxGetThread(); // 得到进程指针 0x0052d6c0
	CWinApp* pApp = AfxGetApp();          // CWinApp 是 CWinThread 的爸 0x0052d6c0

3.用父类对象调用虚函数(实现到哪个子类,就会调用哪个子类哪个虚函数)

 

 

#include <iostream>
using namespace std;
class Parent
{
protected:
	int i;
	int j;
public:
	virtual void Show()
	{
		printf("Parent()\n");
	}
	
	void fun(Parent & p)
	{
		Show();
	}
};

class Child1 : public Parent
{
public:
	virtual void Show()
	{
		printf("Child1\n");
	}
};

class Child2 : public Child1 // 在父类中的构造函数中条用了虚函数 所以当子类实现了 因为多肽就会使用子类的 当子类不实现 就使用父类的
{
public:
// 	virtual void Show()
// 	{
// 		printf("Child2\n");
// 	}
};


int main()
{
	Child1 c1;
	Child2 c2;

// 	Parent *p[] = {&c1, &c2};
// 
// 	p[0]->fun(c1);
// 	p[1]->fun(c2);
// 	p[0]->Show();
// 	p[1]->Show();
	Parent &p = c2;
	p.fun(c2);
	return 0;
}

如果Child2中不实现那个虚函数,在调用的时候回调用父类的函数,所以就像MFC一样,给了用户一个借口,如果用户不实现,程序会默认去使用父类实现的虚函数,如果子类中实现了虚函数,那就使用子类默认的函数.

 

就像MFC CTestApp::InitInstance()中如果在 CTestApp中没有被实现,就会使用

 

BOOL CWinApp::InitInstance()
{
	return TRUE;
}

1.本文部分素材来源网络,版权归原作者所有,如涉及作品版权问题,请与我联系删除。

2.未经原作者允许不得转载本文内容,否则将视为侵权;

3.转载或者引用本文内容请注明来源及原作者;

4.对于不遵守此声明或者其他违法使用本文内容者,本人依法保留追究权等。

下面是我的个人微信公众号,关注【一个早起的程序员】精彩系列文章每天不断。

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个早起的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值