对象指针/当前 this 指针

指向对象的指针,怎么调用对象里面的函数:

指针名 -> 函数名;

#include <iostream>
using namespace std;


class Point{
 public:
 	Point (int x =0, int y = 0):x(x), y(y){};
 	int Get_x(){
 		return x;
 	};
 	int Get_y(){
 	 	return y;
 	 };

 private:

 	 int x;
 	 int y;

};

 int main(){
 	Point number(4,5);
 	Point *p= &number;

 	int a = p -> Get_x(); 
 	int b = number.Get_x(); //这里的两条返回的是一个东西

 	cout << a <<" " << b <<endl;
 	return 0;

 }


this 指针

隐含于类的每一个非静态成员函数中

指出成员函数所操作的对象

    当通过一个对象调用成员函数的时候,系统先将该对象的地址赋给this指针,然后调用成员函数,成团函数对对象的数据成员进行操作的时候,就隐含的使用了this 指针

例如:Point 类的Get_x 函数中的语句:

            return x;

            相当于

             return this->x;

猜你喜欢

转载自blog.csdn.net/weixin_42380877/article/details/80945098