基类用的this指针

时间:2023-03-10 05:55:28
基类用的this指针

结论:基类构造函数中的this指针指向的是派生类的对象

测试代码:

#include <iostream>

using namespace std;

class father;
father *pf; class father
{
public:
father()
{
pf = this;
cout << "基类构造中的this指针: " << this <<endl;
}
}; class son : public father
{
public:
son()
{
//
}
}; son s0; int _tmain(int argc, _TCHAR* argv[])
{
cout << "派生类的全局化对象s0: " << &s0 << endl; son s1;
cout << "派生类的局部化对象s1: " << &s1 << endl; son s2;
cout << "派生类的局部化对象s2: " << &s2 << endl; return ;
}

程序输出:

基类构造中的this指针: 0041A13C
派生类的全局化对象s0: 0041A13C
基类构造中的this指针: 0012FF63
派生类的局部化对象s1: 0012FF63
基类构造中的this指针: 0012FF57
派生类的局部化对象s2: 0012FF57