this指针问题

[复制链接]
 楼主| sinadz 发表于 2012-5-30 19:00 | 显示全部楼层 |阅读模式
#include "stdafx.h"
#include
<cstdlib>
using
namespace std;
struct obj{
    obj(){
        printf(
"%s\n",__FUNCTION__);
    }
};
struct s{
    s(){
        printf(
"%p\n", this);
        i
=2;
        j
=3;
        printf(
"%p\n", this);
    }
   
int i;
   
int j;
};
int main(void){
    s
* ps=new s;
    printf(
"ps=%p\n",ps);
   
return
0;
}



可以打印
00342A10
00342A10
ps=00342A10
为什么流行的C++教材上面总是说,构造函数没有完成之前,this指针没有值?
dfsa 发表于 2012-5-30 19:22 | 显示全部楼层
在调用任何成员函数(除了静态成员函数)的时候,编译器都会将对象的指针传给this。这意味着,程序指针一旦进入任何非静态成员函数,this指针就有值了。
无冕之王 发表于 2012-5-30 19:28 | 显示全部楼层
LS分析的很有道理
您需要登录后才可以回帖 登录 | 注册

本版积分规则

304

主题

2313

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部