《C++起航之离港篇》学习笔记
1. C++语言引用
1.1 引用是什么
举个栗子,一个人姓名叫 罗XX,外号叫 萝卜头儿
引用就是变量的别名
符号:&
不能只有别名,引用不能单独存在
1.2 引用的应用
// *********************
// 基本数据类型的引用
// *********************
#include <iostream>
using namespace std;
int main(void)
{
int a = 3;
int &b = a; // 引用必须初始化,a 的别名称为 b
b = 10; // 对别名的操作,其实就是对本身的操作
cout << a << endl;
return 0;
}
// *********************
// 结构体类型的引用
// *********************
#include <iostream>
using namespace std;
typedef struct
{
int x;
int y;
}Coor; // 结构体 Coor 定义坐标
int main(void)
{
Coor c1;
Coor &c = c1; // c1 结构体的别名为 c
c.x = 10;
c.y = 20;
cout << c1.x << " " << c1.y << endl;
return 0;
}
// *********************
// 指针类型的引用
// 类型 *&指针引用别名 = 指针;
// *********************
#include <iostream>
using namespace std;
int main(void)
{
int a = 10;
int *b = &a; // & 取地址,指针 b 指向 a
int *&c = b; // 指针的引用, b 的别名为 c
*c = 20;
cout << a << endl;
return 0;
}
// *********************
// 引用作为函数参数
// *********************
// C 语言
void fun(int *a, int *b)
{
int c = 0;
c = *a;
*a = *b;
*b = c;
}
int x = 10, y = 20;
fun(&x, &y)
// C++ 语言
void fun(int &a, int &b) // 传入形参取别名
{
int c = 0;
c = a;
a = b;
b = c;
}
int x = 10, y = 20;
fun(x, y)
3. 关键字Const
作用:控制变化
// 常用数据类型
const int x = 5; // x 为常量
// 指针
int x = 5;
const int *p = &x;
*p =3; // 错误,*p 为常量
x= 3; // 正确,编译可通过
int x = 3;
int y = 5;
int *const p = &x;
p = &y; // 错误, p 只能对 x 取地址,不能再指向他人
*p =10; // 正确,可赋值,可修改 x 值
// 引用
int x = 3;
int y = 5;
int const &z = x;
z=10; // 错误,不能给常量赋值
x= 10; // 正确
// 函数
#include <iostream>
using namespace std;
void fun(const int &a, const int &b);
int main(void)
{
int x = 3;
int y = 5;
fun(x,y); // 错误,a, b 均为常量
system("pause");
return 0;
}
void fun(const int &a, const int &b)
{
a = 10;
b = 20;
}
4. 函数新特性
4.1 函数参数默认值
规则:有默认值的参数必须在参数表的最右端
声明函数时定义默认值,定义函数时不设置默认值
无实参使用默认值,否则用实参覆盖默认值
#include <stdlib.h>
#include <iostream>
using namespace std;
void fun(int i = 30, int j = 20, int k = 10); // 从后面往前面一次赋值
int main (void)
{
fun();
fun(100);
fun(100,200);
fun(100,200,300);
system("pause");
return 0;
}
void fun(int i, int j, int k);
{
cout << i << " " << j << " " << k << endl;
}
4.2 函数重载
名称相同,参数可辨
前提:在相同的作用域内,用同一函数名定义的多个函数,参数类型和个数是不同的
好处:例如,求最大值,目的相同,但所求对象的类型、范围不同
#include <iostream>
#include <stdlib.h>
using namespace std;
void fun(int i = 30, int j = 20, int k = 10);
void fun(double i, double j);
int main (void)
{
fun(1.1,2.2); // 调用第二个函数
fun(1,2); // 调用第一个函数,k采用默认值
system("pause");
return 0;
}
void fun(int i, int j, int k);
{
cout << i << " " << j << " " << k << endl;
}
void fun(double i, double j);
{
cout << i << " " << j << endl;
}
4.3 内联函数
目的:节省调用时间
关键字:inline
思考:为什么不把所有函数都设置为内联函数?
原因:(1)内联编译是建议性的,由编译器决定;
(2)逻辑简单,调用频繁的函数使用内联,for循环等复杂的不能使用;
(3)递归函数无法使用内联函数。
#include <iostream>
#include <stdlib.h>
using namespace std;
inline void fun(int i = 30, int j = 20, int k = 10); // 关键字:inline
inline void fun(double i, double j); // 仅在函数声明处添加关键字,其他均相同
int main (void)
{
fun(1.1,2.2); // 调用第二个函数
fun(1,2); // 调用第一个函数,k采用默认值
system("pause");
return 0;
}
void fun(int i, int j, int k);
{
cout << i << " " << j << " " << k << endl;
}
void fun(double i, double j);
{
cout << i << " " << j << endl;
}