C++中的这个指针

要理解“this”指针,了解对象如何查看类的函数和数据成员很重要。

null
  1. 每个对象都有自己的数据成员副本。
  2. 所有人都访问代码段中的相同函数定义。

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

在C++的早期版本中,会让“这个”指针发生变化;通过这样做,程序员可以更改方法正在处理的对象。这个特性最终被删除了,现在C++中的这个值是一个R值。 C++通过调用下面的代码让对象破坏自己:

delete this ;


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

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

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

#include<iostream>
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<iostream>
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<iostream>
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<iostream>
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<iostream>
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<iostream>
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;
}


如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享