首页 > 其他 > 详细

基类用的this指针

时间:2016-02-29 00:15:28      阅读:188      评论:0      收藏:0      [点我收藏+]

结论:基类构造函数中的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 0;
}

程序输出:

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

 

基类用的this指针

原文:http://www.cnblogs.com/ysouyno/p/5225923.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!