-
this 的作用:指向成员函数所作用的对象
-
非静态成员函数中可以直接使用this来代表 指向该函数作用的对象 的指针
-
静态成员函数并不具体作用于某个对象,故不能使用this指针
-
静态成员函数些的参数个数就是真实的参数个数
#include <iostream>
using namespace std;
class Complex{
public:
double real, imag;
void Print(){
cout << real << "," << imag << endl;
}
Complex(double r, double i):real(r), imag(i)
{}
//AddOne作用于一个对象并返回这个对象
Complex AddOne(){
this->real++;
this->Print();
return * this;
}
};
class A{
int i;
public:
void Hello(){
cout << "hello" <<endl;
} //相当于 Hello(A* this){}
};
class B{
int i;
public:
void Hello(){
cout << i << "hello" << endl;
}/*相当于 Hello(B* this){
cout << this->i << "hello" <<endl;
}*/
};
int main(){
Complex c1(1,1), c2(0,0);
c2 = c1.AddOne();
A *p = NULL;
p->Hello(); //成功输出
//报错
//B *q = NULL;
//q->Hello();
return 0;
}