首页 > 代码库 > c++开始学习2
c++开始学习2
慕课学习C++远征之离港篇
1、引用
基本类型引用:int a = 3; int &b = a;
结构体类型引用:typedef struct { int x; int y;}Coor;
Coor c1; Coor &c = c1; c.x = 10; c.y = 20; cout << c1.x << c1.y;
指针类型引用:int a = 10; int *p = &a; int *&q = p; *q = 20; cout << a << endl;
函数引用:fun(int &a, int &b){ }
int x = 10, int y = 20;
fun(x, y)
2、const
基本类型:const int a = 10; // a为常量
指针:const int *p = NULL; int const *p = NULL; 等价
int * const p = NULL;
const int * const p = NULL; int const * const p = NULL; 等价
引用:int x = 10; const int &y = x; // x = 20;正确 // y = 20;错误
3、函数特性
默认值,函数重载(同名函数),内联函数
4、内存管理
申请:new,new *p = new int; new *arr = new int[10];块内存申请与释放。申请要判断是否为空(空为错)
释放:delete,delete p; delete []arr; 释放要设空指针
c++开始学习2