• C++解析五-this 指针,指向类的指针


    C++this指针

    在 C++ 中,每一个对象都能通过 this 指针来访问自己的地址。this 指针是所有成员函数的隐含参数。因此,在成员函数内部,它可以用来指向调用对象。
    友元函数没有 this 指针,因为友元不是类的成员。只有成员函数才有 this 指针。
    下面的实例有助于更好地理解 this 指针的概念:

    #include <iostream>
     
    using namespace std;
     
    class Box
    {
       public:
          // 构造函数定义
          Box(double l=2.0, double b=2.0, double h=2.0)
          {
             cout <<"Constructor called." << endl;
             length = l;
             breadth = b;
             height = h;
          }
          double Volume()
          {
             return length * breadth * height;
          }
          int compare(Box box)
          {
             return this->Volume() > box.Volume();
          }
       private:
          double length;     // Length of a box
          double breadth;    // Breadth of a box
          double height;     // Height of a box
    };
     
    int main(void)
    {
       Box Box1(3.3, 1.2, 1.5);    // Declare box1
       Box Box2(8.5, 6.0, 2.0);    // Declare box2
     
       if(Box1.compare(Box2))
       {
          cout << "Box2 is smaller than Box1" <<endl;
       }
       else
       {
          cout << "Box2 is equal to or larger than Box1" <<endl;
       }
       return 0;
    }

    编译输出:

    kevin@kevin-virtual-machine:~/Documents/Test/C++/five$ ./this 
    Constructor called.
    Constructor called.
    Box2 is equal to or larger than Box1

    C++ Primer Page 258
    引入 this:
    当我们调用成员函数时,实际上是替某个对象调用它。
    成员函数通过一个名为 this 的额外隐式参数来访问调用它的那个对象,当我们调用一个成员函数时,用请求该函数的对象地址初始化 this。例如,如果调用 total.isbn()则编译器负责把 total 的地址传递给 isbn 的隐式形参 this,可以等价地认为编译器将该调用重写成了以下形式:

    //伪代码,用于说明调用成员函数的实际执行过程
    Sales_data::isbn(&total)

    其中,调用 Sales_data 的 isbn 成员时传入了 total 的地址。
    在成员函数内部,我们可以直接使用调用该函数的对象的成员,而无须通过成员访问运算符来做到这一点,因为 this 所指的正是这个对象。任何对类成员的直接访问都被看作是对 this 的隐式引用,也就是说,当 isbn 使用 bookNo 时,它隐式地使用 this 指向的成员,就像我们书写了 this->bookNo 一样。
    对于我们来说,this 形参是隐式定义的。实际上,任何自定义名为 this 的参数或变量的行为都是非法的。我们可以在成员函数体内部使用 this,因此尽管没有必要,我们还是能把 isbn 定义成如下形式:

    std::string isbn() const { return this->bookNo; }

    因为 this 的目的总是指向“这个”对象,所以 this 是一个常量指针(参见2.4.2节,第56页),我们不允许改变 this 中保存的地址。

    指向类的指针

    一个指向 C++ 类的指针与指向结构的指针类似,访问指向类的指针的成员,需要使用成员访问运算符 ->,就像访问指向结构的指针一样。与所有的指针一样,您必须在使用指针之前,对指针进行初始化。
    下面的实例有助于更好地理解指向类的指针的概念:

    #include <iostream>
     
    using namespace std;
    
    class Box
    {
       public:
          // 构造函数定义
          Box(double l=2.0, double b=2.0, double h=2.0)
          {
             cout <<"Constructor called." << endl;
             length = l;
             breadth = b;
             height = h;
          }
          double Volume()
          {
             return length * breadth * height;
          }
       private:
          double length;     // Length of a box
          double breadth;    // Breadth of a box
          double height;     // Height of a box
    };
    
    int main(void)
    {
       Box Box1(3.3, 1.2, 1.5);    // Declare box1
       Box Box2(8.5, 6.0, 2.0);    // Declare box2
       Box *ptrBox;                // Declare pointer to a class.
    
       // 保存第一个对象的地址
       ptrBox = &Box1;
    
       // 现在尝试使用成员访问运算符来访问成员
       cout << "Volume of Box1: " << ptrBox->Volume() << endl;
    
       // 保存第二个对象的地址
       ptrBox = &Box2;
    
       // 现在尝试使用成员访问运算符来访问成员
       cout << "Volume of Box2: " << ptrBox->Volume() << endl;
      
       return 0;
    }

    编译输出:

    kevin@kevin-virtual-machine:~/Documents/Test/C++/five$ ./class_pointer 
    Constructor called.
    Constructor called.
    Volume of Box1: 5.94
    Volume of Box2: 102
  • 相关阅读:
    out/host/linuxx86/obj/EXECUTABLES/aapt_intermediates/aapt 64 32 操作系统
    linux 查看路由器 电脑主机 端口号 占用
    linux proc进程 pid stat statm status id 目录 解析 内存使用
    linux vim 设置大全详解
    ubuntu subclipse svn no libsvnjavahl1 in java.library.path no svnjavahl1 in java.library.path no s
    win7 安装 ubuntu 双系统 详解 easybcd 工具 不能进入 ubuntu 界面
    Atitit.json xml 序列化循环引用解决方案json
    Atitit.编程语言and 自然语言的比较and 编程语言未来的发展
    Atitit.跨语言  文件夹与文件的io操作集合  草案
    Atitit.atijson 类库的新特性设计与实现 v3 q31
  • 原文地址:https://www.cnblogs.com/debruyne/p/9479782.html
Copyright © 2020-2023  润新知