欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > c/c++ >内容正文

c/c++

C++ 引用 Demo - Win32 版

发布时间:2025/4/14 c/c++ 34 豆豆
生活随笔 收集整理的这篇文章主要介绍了 C++ 引用 Demo - Win32 版 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

一 引用

 

C/C++ 禁止在函数调用时直接传递数组的内容,而是强制传递数组指针。
对于结构体和对象没有这种限制,调用函数时既可以传递指针,也可以直接传递内容。
在 C++ 中,有一种比指针更加便捷的传递聚合类型数据的方式,就是引用(Reference)。

引用可以看做是数据的一个别名,通过这个别名和原来的名字都能够找到这份数据。
引用的定义方式类似于指针,只是用&取代了*;

int a = 99;
int &r = a;
变量 r 就是变量 a 的引用,它们用来指代同一份数据;

由于引用 r 和原始变量 a 都是指向同一地址,所以通过引用也可以修改原始变量中所存储的数据,
如果不希望通过引用来修改原始的数据,那么可以在定义时添加 const 限制;

#include <windows.h> #include "resource.h"LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);HINSTANCE hInst; TCHAR szClassName[] = TEXT("refDemo"); char szBuffer[100];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_REFDEMO);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 szBuffer[100];int a = 99;int &r = a;switch (message){case WM_COMMAND:switch (LOWORD(wParam)){case IDM_ref: hdc=GetDC(hwnd);wsprintf(szBuffer, "%d,%d",a,r);TextOut(hdc,10,50,szBuffer,lstrlen(szBuffer));wsprintf(szBuffer, "%#X,%#X",&a,&r);TextOut(hdc,10,80,szBuffer,lstrlen(szBuffer));r=407;wsprintf(szBuffer, "%d,%d",a,r);TextOut(hdc,10,110,szBuffer,lstrlen(szBuffer));break;case IDM_ABOUT:MessageBox (hwnd, TEXT ("refDemo v1.0\nCopyright (C) 2020\n by bo"),TEXT ("refDemo"), 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++引用作为函数参数


在定义或声明函数时,可以将函数的形参指定为引用的形式,这样在调用函数时就会将实参和形参绑定在一起,让它们都指代同一份数据。
如此一来,如果在函数体中修改了形参的数据,那么实参的数据也会被修改,从而拥有“在函数内部影响函数外部数据”的效果。

一个能够展现按引用传参的优势的例子就是交换两个数的值;

#include <windows.h> #include "resource.h"LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);HINSTANCE hInst; TCHAR szClassName[] = TEXT("refDemo");void swap1(int a, int b); void swap2(int *p1, int *p2); void swap3(int &r1, int &r2);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_REFDEMO2);wincl.cbClsExtra = 0;wincl.cbWndExtra = 0;wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);if (!RegisterClassEx (&wincl))return 0;hwnd = CreateWindowEx (0,szClassName,TEXT("C++引用作为函数参数"),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 szBuffer[100];int num1=1000;int num2=3000; switch (message){case WM_COMMAND:switch (LOWORD(wParam)){case IDM_ref: hdc=GetDC(hwnd);swap1(num1, num2);wsprintf(szBuffer, "%d,%d",num1,num2);TextOut(hdc,10,50,szBuffer,lstrlen(szBuffer));swap2(&num1, &num2);wsprintf(szBuffer, "%d,%d",num1,num2);TextOut(hdc,10,80,szBuffer,lstrlen(szBuffer));swap3(num1, num2);wsprintf(szBuffer, "%d,%d",num1,num2);TextOut(hdc,10,110,szBuffer,lstrlen(szBuffer));break;case IDM_ABOUT:MessageBox (hwnd, TEXT ("refDemo v1.0\nCopyright (C) 2020\n by bo"),TEXT ("refDemo"), 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; }//直接传递参数内容 void swap1(int a, int b) {int temp = a;a = b;b = temp; } //传递指针 void swap2(int *p1, int *p2) {int temp = *p1;*p1 = *p2;*p2 = temp; } //按引用传参 void swap3(int &r1, int &r2) {int temp = r1;r1 = r2;r2 = temp; }

swap1() 直接传递参数的内容,不能达到交换两个数的值的目的;

三 C++引用作为函数返回值


引用除了可以作为函数形参,还可以作为函数返回值,
 

#include <windows.h> #include "resource.h"LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);HINSTANCE hInst; TCHAR szClassName[] = TEXT("refDemo");int &plus10(int &);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_REFDEMO3);wincl.cbClsExtra = 0;wincl.cbWndExtra = 0;wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);if (!RegisterClassEx (&wincl))return 0;hwnd = CreateWindowEx (0,szClassName,TEXT("C++引用作为函数返回值"),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 szBuffer[100];int num1 = 99;int num2 = 0; switch (message){case WM_COMMAND:switch (LOWORD(wParam)){case IDM_ref: hdc=GetDC(hwnd);num2 = plus10(num1);wsprintf(szBuffer, "%d,%d",num1,num2);TextOut(hdc,10,50,szBuffer,lstrlen(szBuffer));break;case IDM_ABOUT:MessageBox (hwnd, TEXT ("refDemo v1.0\nCopyright (C) 2020\n by bo"),TEXT ("refDemo"), 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; }int &plus10(int &r) {r += 10;return r; }

 

工程;

 

资源文件;头文件;

#include "resource.h" #include <windows.h>/ // // Menu //IDC_REFDEMO3 MENU BEGINPOPUP "&File"BEGINMENUITEM "C++引用作为函数返回值", IDM_ref MENUITEM "E&xit", IDM_EXITENDPOPUP "&Help"BEGINMENUITEM "&About ...", IDM_ABOUTEND END #define IDM_EXIT 10001 #define IDM_ABOUT 10002#define IDC_REFDEMO3 10101 #define IDD_ABOUTBOX 10102 #define IDM_ref 40001

 

《新程序员》:云原生和全面数字化实践50位技术专家共同创作,文字、视频、音频交互阅读

总结

以上是生活随笔为你收集整理的C++ 引用 Demo - Win32 版的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。