要了解"this"指针, 重要的是要理解对象如何对待类的函数和数据成员。
- 每个对象都有本人的数据成员正本。
- 全副拜访与代码段中雷同的性能定义。
意味着每个对象都有本人的数据成员正本, 并且所有对象共享成员函数的单个正本。
当初的问题是, 如果每个成员函数只有一个正本并且被多个对象应用, 那么如何拜访和更新适当的数据成员?
- 编译器提供隐式指针以及函数名称" this"。
- " this"指针作为暗藏参数传递给所有非动态成员函数调用, 并且可用作所有非动态函数体内的局部变量。
- " this"指针在动态成员函数中不可用, 因为能够在没有任何对象(带有类名)的状况下调用动态成员函数。
- 对于X类, this指针的类型为" X "。另外, 如果X的成员函数申明为const, 则this指针的类型为" const X "
- 在晚期的C ++版本中, " this"指针将被更改;通过这样做, 程序员能够更改办法正在解决的对象。该性能最终被删除, 当初在C ++中为r值。
C ++通过调用以下代码让对象销毁本身:
delete this ;
正如Stroustrup所说, "this"可能是指针的参考, 但在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;}
如果发现任何不正确的中央, 或者想分享无关上述主题的更多信息, 请发表评论。
更多C++开发相干内容请参考:lsbin - IT开发技术:https://www.lsbin.com/
查看以下C++相干的内容:
- C/C++中的函数:https://www.lsbin.com/3559.html
- C++指针和援用的区别:https://www.lsbin.com/3381.html
- C++多线程编程:https://www.lsbin.com/2910.html