1 new操作符

C++中利用==new==操作符在堆区开辟数据
堆区开辟的数据,由程序员手动开辟,手动释放,释放利用操作符 ==delete==
语法:new 数据类型
利用new创建的数据,会返回该数据对应的类型的指针

  1. int* func()
  2. {
  3. int* a = new int(10);
  4. return a;
  5. }
  6. int main()
  7. {
  8. int* p = func();
  9. cout << *p << endl;
  10. cout << *p << endl;
  11. //利用delete释放堆区数据
  12. delete p;
  13. //cout << *p << endl; //报错,释放的空间不可访问
  14. system("pause");
  15. return 0;
  16. }

开辟数组

  1. int main()
  2. {
  3. int* arr = new int[10];
  4. for (int i = 0; i < 10; i++)
  5. {
  6. arr[i] = i + 100;
  7. }
  8. for (int i = 0; i < 10; i++)
  9. {
  10. cout << arr[i] << endl;
  11. }
  12. //释放数组 delete 后加 []
  13. delete[] arr;
  14. system("pause");
  15. return 0;
  16. }

总结:
堆区数据由程序员管理开辟和释放
堆区数据利用new关键字进行开辟内存

2 引用的基本使用

作用: 给变量起别名
语法: 数据类型 &别名 = 原名

  1. int main()
  2. {
  3. int a = 10;
  4. int &b = a;
  5. cout << "a = " << a << endl;
  6. cout << "b = " << b << endl;
  7. b = 100;
  8. cout << "a = " << a << endl;
  9. cout << "b = " << b << endl;
  10. system("pause");
  11. return 0;
  12. }

引用注意事项

  • 引用必须初始化
  • 引用在初始化后,不可以改变

示例:

  1. int main()
  2. {
  3. int a = 10;
  4. int b = 20;
  5. //int &c; //错误,引用必须初始化
  6. int& c = a; //一旦初始化后,就不可以更改
  7. c = b; //这是赋值操作,不是更改引用
  8. cout << "a = " << a << endl;
  9. cout << "b = " << b << endl;
  10. cout << "c = " << c << endl;
  11. system("pause");
  12. return 0;
  13. }

3 引用做函数参数

作用:函数传参时,可以利用引用的技术让形参修饰实参
优点:可以简化指针修改实参

  1. //值传递
  2. void mySwap01(int a, int b)
  3. {
  4. int temp = a; a = b; b = temp;
  5. }
  6. //地址传递
  7. void mySwap02(int* p1, int* p2)
  8. {
  9. int temp = *p1; *p1 = *p2; *p2 = temp;
  10. }
  11. //3. 引用传递
  12. void mySwap03(int& a, int& b)
  13. {
  14. int temp = a; a = b; b = temp;
  15. }
  16. int main()
  17. {
  18. int a = 10;
  19. int b = 20;
  20. //mySwap01(a, b);
  21. //cout << "a:" << a << " b:" << b << endl;
  22. //mySwap02(&a, &b);
  23. //cout << "a:" << a << " b:" << b << endl;
  24. mySwap03(a, b);
  25. cout << "a:" << a << " b:" << b << endl;
  26. system("pause");
  27. return 0;
  28. }

4 引用做函数返回值

  1. //返回局部变量引用
  2. int& test01()
  3. {
  4. int a = 10; //局部变量
  5. return a;
  6. }
  7. //返回静态变量引用
  8. int& test02()
  9. {
  10. static int a = 20;
  11. return a;
  12. }
  13. int main()
  14. {
  15. //不能返回局部变量的引用
  16. int& ref = test01();
  17. cout << "ref = " << ref << endl;
  18. cout << "ref = " << ref << endl;
  19. //如果函数做左值,那么必须返回引用
  20. int& ref2 = test02();
  21. cout << "ref2 = " << ref2 << endl;
  22. cout << "ref2 = " << ref2 << endl;
  23. test02() = 1000;
  24. cout << "ref2 = " << ref2 << endl;
  25. cout << "ref2 = " << ref2 << endl;
  26. system("pause");
  27. return 0;
  28. }

5 引用的本质

  1. //发现是引用,转换为 int* const ref = &a;
  2. void func(int& ref)
  3. { ref = 100; // ref是引用,转换为*ref = 100
  4. }
  5. int main()
  6. {
  7. int a = 10;
  8. //自动转换为 int* const ref = &a; 指针常量是指针指向不可改,也说明为什么引用不可更改
  9. int& ref = a;
  10. ref = 20; //内部发现ref是引用,自动帮我们转换为: *ref = 20;
  11. cout << "a:" << a << endl;
  12. cout << "ref:" << ref << endl;
  13. func(a);
  14. system("pause");
  15. return 0;
  16. }

6 常量引用

作用:常量引用主要用来修饰形参,防止误操作
在函数形参列表中,可以加==const修饰形参==,防止形参改变实参

//引用使用的场景,通常用来修饰形参 
void showValue(const int& v) 
{ 
    //v += 10; 
    cout << v << endl; 
}

int main()
{
    //int& ref = 10;  引用本身需要一个合法的内存空间,因此这行错误
    //加入const就可以了,编译器优化代码,int temp = 10; const int& ref = temp;
    //const int& ref = 10;

    //ref = 100;  //加入const后不可以修改变量
    //cout << ref << endl;

    //函数中利用常量引用防止误操作修改实参
    int a = 100;
    showValue(a);

    system("pause");

    return 0;
}