#include<iostream>
using namespace std;
//引用的本质在c++内部实现,是一个指针常量
//交换函数
//1.值传递
void mySwap01(int a, int b) {
int temp = a;
a = b;
b = temp;
}
//2.地址传递
void mySwap02(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
//3.引用传递
void mySwap03(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
//1.不要返回局部变量的引用
int& test01() {
int a = 10;//局部变量,存放在栈
return a;
}
//2.函数的调用可以作为左值
int& test02() {
static int a = 10;//存放在全局区,在程序结束后系统释放
return a;
}
//打印数据函数
void showValue(const int& value) {
//value = 1000; 防止这种误操作
cout << "value=" << value << endl;
}
int main() {
int a = 10;
//创建引用——起别名——别名的前面加上&——操作的都是同一块内存
int& c = a;//引用必须要初始化;引用一旦初始化后,就不可以更改了
c = 20;
cout << "a=" << a << endl;
cout << "------------------" << endl;
//引用做函数参数
a = 10;
int b = 20;
mySwap01(a, b);//值传递,形参不会修饰实参
cout << "值传递 a=" << a << " b=" << b << endl;//a=10 b=20
cout << "------------------" << endl;
mySwap02(&a, &b);
cout << "地址传递 a=" << a << " b=" << b << endl;
cout << "------------------" << endl;
mySwap03(a, b);
cout << "引用传递 a=" << a << " b=" << b << endl;
cout << "------------------" << endl;
//引用做函数的返回值
int& ref1 = test01();
cout << "ref1=" << ref1 << endl;
//cout << "ref1=" << ref1 << endl;
int& ref2 = test02();
cout << "ref2=" << ref2 << endl;
cout << "------------------" << endl;
//常量引用 const 用来修饰形参防止误操作
const int& ref = 10;//引用必须引一块合法的内存空间
a = 100;
showValue(a);
//system("pause");
return 0;
}
函数的默认参数
#include<iostream>
using namespace std;
//函数的默认参数
//如果自己传入数据,就用自己的数据,如果没有就用默认值
//语法:返回值类型 函数名(形参=默认值){}
int func(int a, int b=20, int c=30) {
return a + b + c;
}
//1.如果一个位置有了默认参数,那么从这个位置开始,从左到右都要有默认值
//2.如果函数声明有默认参数,函数实现就不能有默认参数;是现有声明就别有
int main() {
cout << func(10) << endl;
//system("pause");
return 0;
}
函数的占位参数
#include<iostream>
using namespace std;
//占位参数
//返回值类型 函数名 (数据类型){}
//占位参数 还可以有默认参数
void func(int a, int =10) {//int 是占位用的
cout << "this is func" << endl;
}
int main() {
func(10);
return 0;
}
背景
在看Spring Framework官方文档时,看到这样一段描述:
As of Spring Framework 4.3, an Autowired annotation on such a constructor is no longer necessary if the target bean defines only one constructor to begin with. However, if seve…