传值
传指针
传引用
总的来说,尽量使用传引用。
代码如下:
// 1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
//通过传值来传入参数和传出返回值
int FuncByValue(int x)
{
x += 1;
return x;
}
//通过传指针来传入参数和传出返回值
void FuncByPoint(int* p)
{
*p = *p + 1;
}
//通过引用针来传入参数和传出返回值
void FuncByReference(int& r)
{
r += 1;
}
int _tmain(int argc, _TCHAR* argv[])
{
int n = 0;
cout<<"n 的初始值为:"<<n<<endl;
FuncByValue(n);
cout<<"传值, n = "<<n<<endl;
FuncByPoint(&n);
cout<<"传指针, n = "<<n<<endl;
FuncByReference(n);
cout<<"传引用, n= "<<n<<endl;
return 0;
}