this指针
是那个类构造的对象
这个this指针就是指向那个对象的
比如是派生类构造的对象 this 就是指向派生类的
在A的构造函数中使用了this 但是用其派生类构造对象 所以(有父才有子,先构造个基类)
但this指向的还是派生类滴对象
////////////////////////////////////////////////////////////////////
#include <iostream>
using namespace std;
class A
{
private:
A * ppp;
public :
A()
{
A * pp=this;
ppp=pp;
}
A * fan()
{
return ppp;
}
virtual ~A(){}
virtual void Test1() {cout<<"text1"<<endl;}
void Test2() {cout<<"text1"<<endl;}
};
class B : public A
{
public :
B(){}
virtual ~B(){}
virtual void Test1() {cout<<"text2"<<endl;}
void Test2() {cout<<"text2"<<endl;}
};
B b;
int main(void)
{
A * pd=b.fan();
pd->Test1();
pd->Test2();
return 0;
}


浙公网安备 33010602011771号