徐晓冬

C/C++系列之复杂引用

≯℡__Kan透↙ 提交于 2019-11-28 20:41:23
以struct类型为例: 引用 #include"iostream" #include<string> using namespace std; struct mycoach { string name; int age; }; void printinfo1(mycoach &cpc) { //参数是mycoach实例的别名,指向入参的真实地址,所以一改入参的值也跟着改 cpc.name = "陈培昌"; } void main() { mycoach coach1; coach1.name = "徐晓冬"; printinfo1(coach1); cout << "这位神秘嘉宾是" << coach1.name << endl;//陈培昌 system("pause"); } 输出结果: 形参传入----可以预见这种方式不会改变struct实例的值 #include"iostream" #include<string> using namespace std; struct mycoach { string name; int age; }; void printinfo1(mycoach cpc) { //参数是mycoach实例的别名,指向入参的真实地址,所以一改入参的值也跟着改 cpc.name = "陈培昌"; } void main() { mycoach