最近在学习 C++,所以顺便将每日所学记录下来,一方面为了巩固学习的知识,另一方面也为同样在学习C++的童鞋们提供一份参考。
引用
普通引用
变量名实质上是一段连续存储空间的别名,是一个标号(门牌号)
程序中通过变量来申请并命名内存空间
通过变量的名字可以使用存储空间
引用概念
a) 在C++中新增加了引用的概念
b) 引用可以看作一个已定义变量的别名
c) 引用的语法:Type& name = var;
d) 引用做函数参数那?(引用作为函数参数声明时不进行初始化)
普通引用在声明时必须用其它的变量进行初始化,引用很像一个只读的常量
引用作左值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #include <iostream> using namespace std;
int getA() { int a; a = 10; return a; }
int& getB() { int a; a = 10; return a; }
int main() { int a1 = 0; int a2 = 0; a1 = getA(); a2 = getB(); int &a3 = getB();
printf("a1 = %d a2 = %d a3 = %d\n", a1, a2, a3); system("pause"); return 0; }
|
当被调用的函数当左值的时候,必须返回一个引用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| #include <iostream> using namespace std;
int g1(int *p) { *p = 100; return *p; }
int &g2(int *p) { *p = 200; return *p; } int main() { int a = 10; int a1 = g1(&a); int a2 = g2(&a); int &a3 = g2(&a); printf("a1 = %d a2 = %d a3 = %d\n", a1, a2, a3); system("pause"); return 0; }
|
指针引用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #include <iostream> using namespace std;
struct Teacher { char name[64]; int age; }; int getTeacher01(Teacher **myp) { Teacher *p = (Teacher *)malloc(sizeof(Teacher)); memset(p, 0, sizeof(Teacher)); p->age = 100; *myp = p; return 0; }
int getTeacher02(Teacher* &myp) { Teacher *p = (Teacher *)malloc(sizeof(Teacher)); memset(p, 0, sizeof(Teacher)); p->age = 100; myp = p; return 0; }
int main() { Teacher *p = NULL; getTeacher02(p); printf("age = %d\n", p->age); system("pause"); }
|
常引用
使用变量初始化const引用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| using namespace std;
struct Teacher { char name[64]; int age; };
void print01(Teacher &t1) { t1.age = 200; }
void print02(const Teacher &t1) { //t1.age = 200; //const修饰后不可修改 }
int main() { int a = 10; const int &b = a; a = 11; //b = 22; //const修饰的变量不可以修改 Teacher t1; t1.age = 100; printf("age = %d\n", t1.age); print(t1); printf("age = %d\n", t1.age); system("pause"); return 0; }
|
const引用让变量拥有只读属性
使用字面量常量初始化const引用
1 2 3 4 5 6 7 8 9 10 11 12 13
| int main() { const int &a = 10; int *p = (int *)&a;
*p = 12; printf("a:%d", a);
system("pause"); return 0; }
|
当使用常量(字面量)对const引用进行初始化时,C++编译器会为常量值分配空间,并将引用名作为这段空间的别名
使用常量对const引用初始化后将生成一个只读变量
默认参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #include <iostream> using namespace std;
void PrintA(int x = 3) { printf("x = %d\n", x); }
void PrintB(int a, int b = 2, int c = 3) { printf("a = %d b = %d c = %d\n", a, b, c); }
void PrintC(int,int,int) { int x = 10; printf("x = %d\n", x); }
int main() { int a = 10; PrintA(); PrintA(1); PrintB(10); PrintB(a, 20,30); PrintC(a,a,a); system("pause"); return 0; }
|
当参数中存在默认参数时,那么后面的参数必须存在默认赋值
函数重载
基础函数重载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| #include <iostream> using namespace std;
void Print(int a) { printf("a = %d\n", a); } void Print(int a,int b) { printf("a = %d b = %d\n", a, b); }
void Print(int a,double b) { printf("a = %d b = %lf\n", a); }
void Print(int a,char *p) { printf("a = %d p = %s\n", a,p); }
int main() { Print(1); Print(1, 2); Print(1, 3.14); Print(1, "HelloWorld"); system("pause"); return 0; }
|
函数重载遇上默认参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <iostream> using namespace std;
int Print(int a, int b) { printf("a = %d b = %d", a, b); return a; }
int Print(int a, int b,int c = 20) { printf("a = %d b = %d", a, b); return a; }
int main() { int c; c = Print(10, 20); system("pause"); return 0; }
|