C++程序演示类中this关键字的使用


C++ 中的 ‘this’ 关键字非常重要,它在多种使用场景中都有应用。‘this’ 关键字或 ‘this’ 指针在对象成员函数被调用时用作隐式对象参数,并指向调用该函数的对象。我们将了解 ‘this’ 关键字的不同使用场景。

语法

‘this’ 关键字的用法如下所示

this->variable_name;

使用场景 1:解决变量遮蔽

变量遮蔽是 ‘this’ 指针非常常见的应用场景之一。当类成员变量与另一个参数变量或局部变量具有相同的名称时,就会发生变量遮蔽。局部变量“遮蔽”了类成员变量。为了引用类成员变量,我们使用 ‘this’ 关键字。

语法

int value;
public:
   void demoFunction(int value) {
      this->value = value;
   }

从语法中可以看出,使用 ‘this’ 关键字引用的变量是类成员变量,而另一个变量是仅在局部范围内可用的参数变量。

示例

#include <iostream>
using namespace std;

class Test {
   //this is a class member variable
   string testString;
   public:

      //non-static member function
      void setData(string testString) {

         //this is refering to the class member variable
         this->testString = testString;
      }
      
      void getData() {
         cout << "The string is: " << this->testString << endl;
      }
};
int main() {
   //create the object
   Test test;

   //call the member function
   test.setData("This is a test for variable shadowing!");
   test.getData();
   return 0;
}

输出

The string is: This is a test for variable shadowing!

使用场景 2:访问成员变量

this’ 指针还可以用于访问类内部的成员函数和变量。在前面的示例中,我们看到了 ‘this’ 如何解决变量遮蔽问题。如果我们必须在另一个类成员函数内部引用类的另一个成员函数,则使用 ‘this’ 指针。语法和示例如下所示。

语法

void demoFunction1() {};
void demoFunction2() {
   this->demoFunction1();
}

从语法中可以看出,使用 ‘this’ 关键字引用的变量是类成员变量,而另一个变量是仅在局部范围内可用的参数变量。

示例

#include <iostream>
using namespace std;

class Test {
   public:
      //this is a public class member variable
      string testString;

      //non-static member function
      void setData(string testString) {

         //this is refering to the class member variable
         this->testString = testString;
      }
      
      void getAndPrint(string str) {
         //accessing both member variables and functions using this pointer
         this->setData(str);
         cout << "The string is: " << this->testString << endl;
      }
};
int main() {
   //create the object
   Test test;

   //call the member function
   test.getAndPrint("This is a test for member accession!");
   return 0;
}

输出

The string is: This is a test for member accession!

使用场景 3:访问对象

我们可以使用 ‘this’ 关键字访问当前内存中的对象,并进一步操作它们。在下面的示例中,我们将使用成员函数和 ‘this’ 指针删除当前对象。

语法

void demoFunction() {
   delete this;
}

示例

#include <iostream>
using namespace std;

class Test {
   public:
      //this is a public class member variable
      string testString;

      //non-static member function
      void setData(string testString) {

         //this is refering to the class member variable
         this->testString = testString;
      }
      
      void getAndPrint(string str) {
         //accessing both member variables and functions using this pointer
         this->setData(str);
         cout << "The string is: " << this->testString << endl;
      }
      
      void delObject() {
         //accessing the current object and deleting it
         delete this;
      }
      
};
int main() {
   //create the object
   Test test;

   //call the member function
   test.getAndPrint("This is a test for accessing objects!");
   test.delObject();
   test.getAndPrint("This is a test for accessing objects!");
   return 0;
}

输出(错误)

The string is: This is a test for accessing objects!
munmap_chunk(): invalid pointer
timeout: the monitored command dumped core
sh: line 1: 321122 Aborted                 /usr/bin/timeout 10s main

在此示例中,我们使用 ‘this’ 指针删除了当前对象,然后再次尝试正常使用 ‘getAndPrint()’ 函数。我们可以看到,当它运行时,会引发“段错误”,因为该对象已从内存中删除。

结论

从各种使用场景中可以看出,‘this’ 指针对于 C++ 中的各种操作非常有用。‘this’ 指针主要用于访问当前分配对象的实例变量。需要注意的是,‘this’ 仅适用于非静态成员函数。它不适用于非类函数,并且在使用时始终返回调用对象的地址。

更新于: 2022年12月13日

5K+ 次浏览

开启你的职业生涯

通过完成课程获得认证

立即开始
广告

© . All rights reserved.