#include <iostream>
using namespace std;
void func1(int no, string str) //传值
{
no = 8;
str = "我有一只小小鸟";
cout << "亲爱的" << no << "号," << str << endl;
}
void func2(int* no, string* str) //传地址
{
*no = 3;
*str = " 我有一只小小鸟。";
cout << "亲爱的" << *no << "号," << *str << endl;
}
void func3(int& no, string& str)//传引用 参数为引用,代码同传值一样
{
no = 3;
str= " 我有一只小小鸟。";
cout << "亲爱的" << no << "号," << str << endl;
}
int main()
{
int bh = 38;
string message = "我是猪。";
//func1(bh,message); //传值的方法。 修改形参,不会改变实参的值。
//func2(&bh, &message); //传地址的方法。 修改形参,会改变实参的值。
func3(bh, message); //传引用的方法。修改形参,会改变实参的值。 使用方法同传值一样
cout << "亲爱的" << bh << "号," << message << endl;
}
C++ 函数传值、传指针、传引用
于 2022-12-05 21:10:44 首次发布