[C++]函数参数浅析
2014-01-09 00:03 hellenism 阅读(163) 评论(0) 编辑 收藏 举报Date:
2014-1-9
Summary:
函数参数相关记录
Contents:
1.形参实参
形参:用于接收值的变量被称为形参
实参:传递给函数的值被称为实参
2.函数的参数传递之后2种
a).值传递.将实参的值拷贝之后,传递给形参(如果是指针,则拷贝指针的值,传递给形参)
b).引用传递.如果形参是引用,则形参被绑定到了对应的实参上
Notes:
1.使用引用作为形参,尽量避免拷贝
2.函数参数,非引用传递,则为拷贝值传递
EX:
1 // 2 // main.cpp 3 // Cpp_All_Test 4 // 5 // Created by StephenLee on 1/1/14. 6 // Copyright (c) 2014 StephenLee. All rights reserved. 7 // 8 9 void mySwap(int* pNum1,int* pNum2) 10 { 11 int temp = (*pNum1); 12 (*pNum1) = (*pNum2); 13 (*pNum2) = temp; 14 } 15 16 void mySwap(int& num1,int& num2) 17 { 18 int temp = num1; 19 num1 = num2; 20 num2 = temp; 21 } 22 23 24 int nNum1 = 10; 25 int nNum2 = 20; 26 cout << "nNum1:" << nNum1 << "\nnNum2:" << nNum2 << endl; 27 mySwap(&nNum1,&nNum2); 28 29 cout << "after mySwap \nnNum1:" << nNum1 << "\nnNum2:" << nNum2 << endl; 30 31 mySwap(nNum1,nNum2); 32 cout << "after mySwap \nnNum1:" << nNum1 << "\nnNum2:" << nNum2 << endl;
THE END
人生就是一局不能Again的DOTA