实验: 函数指针作为函数入参

    技术2024-08-21  79

    // ThreadNoDefaultLibTest.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <process.h> #include "ThreadNoDefaultLibTest.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif / // The one and only application object CWinApp theApp; using namespace std; typedef VOID (*PFNMonitorThread)(PVOID); typedef INT (__stdcall *PFNAdd)(INT a, INT b); void CreateMonitorThread(PVOID pFnThread); void CreateMonitorThread2(PFNMonitorThread pFnThread); void CallFpAdd(PFNAdd pFnAdd, INT a, INT b); INT __stdcall Add(INT a, INT b); void __cdecl MonitorThread(PVOID pParam); int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; // initialize MFC and print and error on failure if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs cerr << _T("Fatal Error: MFC initialization failed") << endl; nRetCode = 1; } else { // TODO: code your application's behavior here. CString strHello; strHello.LoadString(IDS_HELLO); cout << (LPCTSTR)strHello << endl; CallFpAdd(Add, 66, 88); CreateMonitorThread2(MonitorThread); CreateMonitorThread(MonitorThread); } Sleep(5000); return nRetCode; } void CreateMonitorThread(PVOID pFnThread) { PFNMonitorThread pFn = (PFNMonitorThread)pFnThread; _beginthread(pFn, 0, NULL); } void CreateMonitorThread2(PFNMonitorThread pFnThread) { _beginthread(pFnThread, 0, NULL); } void __cdecl MonitorThread(PVOID pParam) { while(1) { Beep(100, 200); Sleep(100); //printf("."); } } void CallFpAdd(PFNAdd pFnAdd, INT a, INT b) { INT nRc = 0; nRc = pFnAdd(a, b); printf("Add([%d], [%d]) = %d/n", a, b, nRc); } INT __stdcall Add(INT a, INT b) { return (a + b); }

     

    最新回复(0)