#include <iostream>
using namespace std;
typedef struct {
int x;
int y;
}Coord;
void fun(int&, int&);
int main() {
int a = 10;
int& b = a;
b = 20;
cout << a << endl;
a = 30;
cout << b << endl;
Coord c;
Coord& c1 = c;
c1.x = 10;
c1.y = 20;
cout << c.x << endl;
cout << c.y << endl;
int a = 3;
int* p = &a;
int*& q = p;
*q = 5;
cout << a << endl;
int x = 10;
int y = 20;
cout << x << ", " << y << endl;
fun(x, y);
cout << x << ", " << y << endl;
return 0;
}
void fun(int& a, int& b) {
int c;
c = a;
a = b;
b = c;
}
2.const
a.概念
控制变化的关键字
b.作用范围
基本数据类型
指针
引用
函数参数
3.函数默认参数
规则
参数一定要从右边开始,不能有间隔
4.函数重载
函数名一样
参数列表不一样
5.内联函数
使用条件
代码段替换,没有调用的过程
inline
建议性的,有编译器决定
逻辑简单,调用频繁(没有循环的)
6.内存管理
内存是一种资源,有操作系统关系,程序只能向系统申请,归还
C:malloc/free
C++:new/delete
单个数据/数据
Demo:
#include <iostream>
using namespace std;
int mian() {
int *p = new int(10);
if (p == NULL) {
return 0;
}
cout << *p << endl;
delete p;
p = NULL;
return 0;
}
主要内容引用#define const函数默认值 函数重载内存管理1.引用a.概念引用就是变量的别名,引用必须要初始化,因为没有本体,别名无法处在b.类型基本数据类型结构体指针类型作为函数参数Demo#include <iostream>using namespace std;typedef struct { int x; int y;}Coord;void fu