📜  C++中的“ this”指针

📅  最后修改于: 2021-05-25 21:43:13             🧑  作者: Mango

要了解“此”指针,重要的是要了解对象如何看待类的函数和数据成员。

  1. 每个对象都有自己的数据成员副本。
  2. 全部访问与代码段中存在的功能定义相同的函数。

意味着每个对象都有自己的数据成员副本,并且所有对象共享成员函数的单个副本。
现在的问题是,如果每个成员函数只有一个副本并且被多个对象使用,那么如何访问和更新适当的数据成员?
编译器提供隐式指针以及函数名称作为“ this”。
“ this”指针作为隐藏参数传递给所有非静态成员函数调用,并且可用作所有非静态函数主体内的局部变量。 “ this”指针在静态成员函数中不可用,因为可以在没有任何对象(带有类名)的情况下调用静态成员函数。
对于X类,此指针的类型为’X *’。另外,如果将X的成员函数声明为const,则此指针的类型为’const X *’(请参阅此GFact)

在早期的C++版本中,“ this”指针将被更改。通过这样做,程序员可以更改方法正在处理的对象。该功能最终被删除,现在在C++中为r值。
C++通过调用以下代码让对象销毁自身:

delete this;

正如Stroustrup所说的,“ this”可能是指针的引用,但是在C++的早期版本中没有引用。如果将“ this”实现为参考,则可以避免上述问题,并且比指针更安全。

以下是使用“ this”指针的情况:

1)当局部变量的名称与成员的名称相同时

#include
using namespace std;
  
/* local variable is same as a member's name */
class Test
{
private:
   int x;
public:
   void setX (int x)
   {
       // The 'this' pointer is used to retrieve the object's x
       // hidden by the local variable 'x'
       this->x = x;
   }
   void print() { cout << "x = " << x << endl; }
};
  
int main()
{
   Test obj;
   int x = 20;
   obj.setX(x);
   obj.print();
   return 0;
}

输出:

x = 20

对于构造函数,当参数名称与成员名称相同时,也可以使用初始化程序列表。

2)返回对调用对象的引用

/* Reference to the calling object can be returned */ 
Test& Test::func ()
{
   // Some processing
   return *this;
} 

当返回对本地对象的引用时,返回的引用可用于链接单个对象上的函数调用

#include
using namespace std;
  
class Test
{
private:
  int x;
  int y;
public:
  Test(int x = 0, int y = 0) { this->x = x; this->y = y; }
  Test &setX(int a) { x = a; return *this; }
  Test &setY(int b) { y = b; return *this; }
  void print() { cout << "x = " << x << " y = " << y << endl; }
};
  
int main()
{
  Test obj1(5, 5);
  
  // Chained function calls.  All calls modify the same object
  // as the same object is returned by reference
  obj1.setX(10).setY(20);
  
  obj1.print();
  return 0;
}

输出:

x = 10 y = 20

锻炼:
预测以下程序的输出。如果存在编译错误,请修复它们。

问题1

#include
using namespace std;
  
class Test
{
private:
  int x;
public:
  Test(int x = 0) { this->x = x; }
  void change(Test *t) { this = t; }
  void print() { cout << "x = " << x << endl; }
};
  
int main()
{
  Test obj(5);
  Test *ptr = new Test (10);
  obj.change(ptr);
  obj.print();
  return 0;
}

问题2

#include
using namespace std;
  
class Test
{
private:
  int x;
  int y;
public:
  Test(int x = 0, int y = 0) { this->x = x; this->y = y; }
  static void fun1() { cout << "Inside fun1()"; }
  static void fun2() { cout << "Inside fun2()"; this->fun1(); }
};
  
int main()
{
  Test obj;
  obj.fun2();
  return 0;
}

问题3

#include
using namespace std;
  
class Test
{
private:
  int x;
  int y;
public:
  Test (int x = 0, int y = 0) { this->x = x; this->y = y; }
  Test setX(int a) { x = a; return *this; }
  Test setY(int b) { y = b; return *this; }
  void print() { cout << "x = " << x << " y = " << y << endl; }
};
  
int main()
{
  Test obj1;
  obj1.setX(10).setY(20);
  obj1.print();
  return 0;
}

问题4

#include
using namespace std;
  
class Test
{
private:
  int x;
  int y;
public:
  Test(int x = 0, int y = 0) { this->x = x; this->y = y; }
  void setX(int a) { x = a; }
  void setY(int b) { y = b; }
  void destroy()  { delete this; }
  void print() { cout << "x = " << x << " y = " << y << endl; }
};
  
int main()
{
  Test obj;
  obj.destroy();
  obj.print();
  return 0;
}
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”