天天看點

C++函數模闆Demo - win32 版

C++ 模闆

模闆是泛型程式設計的基礎,泛型程式設計即以一種獨立于任何特定類型的方式編寫代碼。

模闆是建立泛型類或函數的藍圖或公式。庫容器,比如疊代器和算法,都是泛型程式設計的例子,它們都使用了模闆的概念。

每個容器都有一個單一的定義,比如 向量,我們可以定義許多不同類型的向量,比如 vector <int> 或 vector <string>。

可以使用模闆來定義函數和類。

函數模闆

模闆函數定義的一般形式如下所示:

template <typename type> ret-type func-name(parameter list)

{

   // 函數的主體

}

type 是函數所使用的資料類型的占位符名稱。這個名稱可以在函數定義中使用。

#include <windows.h>
#include "resource.h"
#include <string>

using namespace std;
 
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
 
HINSTANCE hInst;
TCHAR szClassName[] = TEXT("tempDemo");

template <typename T>
inline T const& Max (T const& a, T const& b) 
{ 
    return a < b ? b:a; 
} 
 
int WINAPI
WinMain (HINSTANCE hThisInstance,
         HINSTANCE hPrevInstance,
         LPSTR lpszArgument,
         int nFunsterStil)
{
    HWND hwnd;
    MSG messages;
    WNDCLASSEX wincl;
 
	hInst = hThisInstance;
	
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;
    wincl.style = CS_DBLCLKS;
    wincl.cbSize = sizeof (WNDCLASSEX);
 
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = MAKEINTRESOURCE (IDC_TEMPDEMO1);
    wincl.cbClsExtra = 0;
    wincl.cbWndExtra = 0;
    wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
 
    if (!RegisterClassEx (&wincl))
        return 0;
 
    hwnd = CreateWindowEx (
           0,
           szClassName,
           TEXT("C++函數模闆Demo"),
           WS_OVERLAPPEDWINDOW,
           CW_USEDEFAULT,
           CW_USEDEFAULT,
           300,
           200,
           HWND_DESKTOP,
           NULL,
           hThisInstance,
           NULL
           );
 
    ShowWindow (hwnd, nFunsterStil);
 
    while (GetMessage (&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }
 
    return messages.wParam;
}
 
LRESULT CALLBACK
WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;
	RECT rt;	
	char dstr[20];
	char szBuffer[100];	
	int i = 99;
	int j = 60;
	double f1=17.98;
	double f2=77.09;
	string s1 = "Hello"; 
    string s2 = "World";
					
    switch (message)
    {
			case WM_COMMAND:
    		switch (LOWORD(wParam))
    		{
			case IDM_temp:		
				hdc=GetDC(hwnd);
				wsprintf(szBuffer, "Max(i, j): %d",Max(i, j));
				TextOut(hdc,80,30,szBuffer,lstrlen(szBuffer));
				//cout << "Max(f1, f2): " << Max(f1, f2) << endl;
				sprintf(dstr,"%.2lf",Max(f1, f2));
				wsprintf(szBuffer, "Max(f1, f2): %s",dstr);
				TextOut(hdc,80,60,szBuffer,lstrlen(szBuffer));
				wsprintf(szBuffer, "Max(s1, s2): %s",(Max(s1, s2)).c_str());
				TextOut(hdc,80,90,szBuffer,lstrlen(szBuffer));
				break;
		    case IDM_ABOUT:
				MessageBox (hwnd, TEXT ("tempDemo v1.0\nCopyright (C) 2020\n by bo"),
                        TEXT ("tempDemo"), MB_OK | MB_ICONINFORMATION);
				break;
			case IDM_EXIT:
				DestroyWindow(hwnd);
				break;
			default:
				return DefWindowProc(hwnd, message, wParam, lParam);	    		
		    }
    		break;
  		case WM_CREATE:
  			break;
    	case WM_PAINT:
			hdc = BeginPaint(hwnd, &ps);					
			GetClientRect(hwnd, &rt);				
			EndPaint(hwnd, &ps);
			break;
        case WM_DESTROY:
            PostQuitMessage (0);
            break;
        default:
            return DefWindowProc (hwnd, message, wParam, lParam);
    }
 
    return 0;
}
           

運作;

C++函數模闆Demo - win32 版
C++函數模闆Demo - win32 版

這裡還有一個問題;

第三次是比較兩個字元串的大小;代碼為:

wsprintf(szBuffer, "Max(s1, s2): %s",(Max(s1, s2)).c_str());

對于Max傳回的string再調用c_str()函數;不如此,将出現錯誤:

cannot pass objects of non-POD type `const struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >' through `...'; call will abort at runtime

因為;

1 printf函數輸出字元串是針對char *的,即printf隻能輸出c語言的内置資料類型,而string不是c語言的内置資料類型。

2 string類型的對象不止包含字元串,還包含了許多用于操作的函數,是以&str并非字元串的首位址。

3 如需輸出string對象中的字元串,可以使用string的成員函數c_str(),該函數傳回字元串的首字元的位址。

可參閱;

https://blog.csdn.net/li_l_il/article/details/84196587

工程;

C++函數模闆Demo - win32 版
C++函數模闆Demo - win32 版

資源檔案,頭檔案;

#include "resource.h"
#include <windows.h>

/
//
// Menu
//

IDC_TEMPDEMO1 MENU 
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "C++ 函數模闆Demo",                	IDM_temp
        MENUITEM "E&xit",                	IDM_EXIT
    END
    POPUP "&Help"
    BEGIN
        MENUITEM "&About ...",           	IDM_ABOUT
    END
END
           
#define 	IDM_EXIT		10001
#define 	IDM_ABOUT		10002

#define 	IDC_TEMPDEMO1		10101
#define 	IDD_ABOUTBOX	10102
#define     IDM_temp     40001