• C++对引用和指针的理解、应用



    前言

    本篇只记录了理解并应用 “指针、引用” 知识点的过程,文章中有些小例子代码冗杂,对指针、引用反复使用的目的仅仅是为了详细的展现 “指针、引用” 的使用方式。

    内容涉及:结构体、引用、指针、函数、类和对象

    一、简介

    1.引用相当于一个变量的别名

        int jkvalue=10;
        int& cite_value=jkvalue;
    
    • 1
    • 2

    2.指针相当于指向了一个变量的地址

        int jkvalue=11;
        int* pjkvalue=&jkvalue;
    
    • 1
    • 2

    3.示例

    引用和指针1

    注:此处的&表示取jkvalue变量的地址,pjkvalue是指针,指向jkvalue这个变量的地址,类型是int*(int类型的指针),通过*point_value来获得jkvalue的引用

    二、结构体

    1.定义并初始化结构体

        //结构体定义
        struct Book {
            char Name[50];
            int ID;
            double price;
        }book;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
        //初始化结构体
        Book mathbook;
        strcpy_s(mathbook.Name, "shuxue");
        mathbook.ID = 10086;
        mathbook.price = 12.56;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.引用结构体

    • 代码

          //结构体作为参数输出详细信息
          void printDetail(struct Book book) {
              cout << "书名:" << book.Name << endl;
              cout << "ID:" << book.ID << endl;
              cout << "价格:" <<book.price << endl;
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
          //引用结构体并作为参数传入函数
          Book& sameMathbook = mathbook;
          printDetail(sameMathbook);
      
      • 1
      • 2
      • 3
    • 执行结果
      引用和指针2

    3.指针指向结构体

    • 代码

          //结构体指针作为参数输出详细信息
          void printDetail(struct Book* book) {
              cout << "指针作为参数:"<< endl;
              cout << "书名:" << book->Name << endl;
              cout << "ID:" << book->ID << endl;
              cout << "价格:" << book->price << endl;
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
          //指针指向一个结构体变量
          Book* bookpoint = &mathbook;
          printDetail(bookpoint);
      
      • 1
      • 2
      • 3
    • 执行结果
      引用和指针3

    三、类、对象

    1.类的定义

        class Vehicle {
            public://属性
                char Brand[10];
                double price;
                int siteNum;
            Vehicle(char* id,double newprice,int sitenum) {//构造函数
                strcpy_s(Brand, id);//char*转char[]拷贝  char[]转char*直接赋值
                price = newprice;
                siteNum = sitenum;
            }
            public:
                void printVehicleInfo();//成员函数定义
        };
    
        void Vehicle::printVehicleInfo() {//实现成员函数printVehicleInfo
            cout << "品牌:" << this->Brand << endl;
            cout << "价格:" << this->price << endl;
            cout << "承载人数:" << this->siteNum << endl;
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    2.引用一个类的对象实例

    • 代码

          //对printCarInfo重载,可以传入指针
          void printCarInfo(Vehicle* vehicle) {
              cout << "品牌:" << vehicle->Brand << endl;
              cout << "价格:" << vehicle->price << endl;
              cout << "承载人数:" << vehicle->siteNum << endl;
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
          //初始化对象实例
          char vehicleBrand[10] = "Benz";
          Vehicle mycar(vehicleBrand, 187536.25, 6);
          
          //引用对象,并将引用传入函数printCarInfo
          Vehicle& cite_mycar = mycar;
          printCarInfo(cite_mycar);
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    • 执行结果
      引用和指针4

    3.指针指向一个类的对象实例

    • 代码

          //对printCarInfo重载,可以传入引用
          void printCarInfo(Vehicle& vehicle) {
              cout << "品牌:" << vehicle.Brand << endl;
              cout << "价格:" << vehicle.price << endl;
              cout << "承载人数:" << vehicle.siteNum << endl;
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
          //指针指向对象实例,并将指针传入函数printCarInfo
          Vehicle* point_mycar = &mycar;
          printCarInfo(point_mycar);
      
      
      • 1
      • 2
      • 3
      • 4
    • 执行结果
      引用和指针5

    四、作为函数的参数

    三、1 中有对Vehicle类的定义

    1.引用作为函数参数

    传递引用作为函数参数时,如果是对象或结构体,使用”.”来访问内部元素,修改引用的值会影响到原值,相当于直接传入的实参,而不是形参

    • 代码
          Vehicle& copyCarInfo(Vehicle& vehicle) {
              strcpy_s(vehicle.Brand, "MINI");
              vehicle.price = 300000;
              return vehicle;
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
          Vehicle myminicar;//调用默认构造函数
          Vehicle& minicarinfo = copyCarInfo(myminicar);//这里等于是Vehicle& vehicle=myminicar
          printCarInfo(minicarinfo);
      
      • 1
      • 2
      • 3
    • 执行结果
      引用和指针6

    2.指针作为函数参数

    传递引用作为函数参数时,如果是对象或结构体,使用”->”来访问内部元素,修改指针指向地址对应的元素会影响到原来的值

    • 代码
          Vehicle& copyCarInfo(Vehicle* vehicle) {
              strcpy_s(vehicle->Brand, "BMW-MINI");
              vehicle->price = 300000;
              return *vehicle;
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
          Vehicle myBMWminicar;
          Vehicle* pmyBMWminicar=&myBMWminicar;
          Vehicle& bmwminicarinfo = copyCarInfo(pmyBMWminicar);
          printCarInfo(bmwminicarinfo);
      
      • 1
      • 2
      • 3
      • 4
    • 执行结果
      引用和指针7

    五、作为函数的返回值

    1.引用作为函数的返回值

    • 示例

          Vehicle& copyCarInfo(Vehicle& vehicle) {
              strcpy_s(vehicle.Brand, "MINI");
              vehicle.price = 300000;
              return vehicle;
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
          Vehicle myBMWminicar;
          Vehicle* pmyBMWminicar = &myBMWminicar;
          Vehicle& bmwminicarinfo = copyCarInfo(pmyBMWminicar);
          printCarInfo(bmwminicarinfo);
      
      • 1
      • 2
      • 3
      • 4
    • 注意:错误示范
      这里要注意返回引用的作用域,如果引用的是函数内部定义的局部变量,返回的引用则是乱码

          Vehicle& copyCarInfo() {//这里面citenewvehicle是局部变量的引用
              Vehicle newvehicle;
              Vehicle& citenewvehicle = newvehicle;
              strcpy_s(newvehicle.Brand, "MINI");
              newvehicle.siteNum = 4;
              return citenewvehicle;
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7

      执行结果
      引用和指针8

    2.指针作为函数的返回值

    • 示例

          Vehicle* copyCarInfo(Vehicle& vehicle) {
              Vehicle* pnewvehicle = &vehicle;
              strcpy_s(vehicle.Brand, "MINI-point");
              vehicle.price = 200000;
              return pnewvehicle;
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
          Vehicle myminicar;
          Vehicle* minicarinfo = copyCarInfo(myminicar);
          printCarInfo(minicarinfo);
      
      • 1
      • 2
      • 3

      执行结果
      引用和指针9

    • 注意:错误示范
      和(1)同样需要注意的是:
      当指针指向一个局部变量时,返回这个指针会出现乱码,要注意生命周期。

         Vehicle* copyCarInfo(Vehicle& vehicle) {
              Vehicle newvehicle;
              Vehicle* pnewvehicle = &newvehicle;
              strcpy_s(newvehicle.Brand, "MINI-point");
              newvehicle.price = 200000;
              return pnewvehicle;
          } 
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
          Vehicle myminicar;
          Vehicle* minicarinfo = copyCarInfo(myminicar);
          printCarInfo(minicarinfo);
      
      • 1
      • 2
      • 3

      执行结果
      引用和指针10

  • 相关阅读:
    使用RabbitMQ死信交换机实现延迟消息
    Spring原理学习(六)Scope
    Mac中Git如何忽略.DS_Store文件
    C语言日记——调试篇
    数据分析篇-数据认知分析
    服务器被黑该如何查找入侵痕迹以及如何防御攻击
    ViewModel 记录下 +
    5-2 Pytorch中的模型层layers
    电脑重装系统Win11格式化硬盘的详细方法
    第七章TCP/IP——ARP网络攻击与欺骗
  • 原文地址:https://blog.csdn.net/hehe_soft_engineer/article/details/126865153