faku 发表于 2012-05-17 22:23

谁能给我简要说一下C++编译器的this指针式如何实现的?

这个this是运行时候地址, 如何得到?

EricFisher 发表于 2012-05-18 09:25

很久以前看过一点《深度探索 C++ 对象模型》里面讲的还算比较透彻,不妨找来读读。

folklore 发表于 2012-05-20 19:36

回复 1# faku
class ca{
public:
int out(){
   printf("%p",this);
}
};
you can translate the above code to c
struct sa{
   
};
int out(sa *pthis){
   printf("%p",pthis);
}
or simulating as c++
struct sa{
   int (*m_out)(sa *);
};
static int out(sa *pthis){
   printf("%p",pthis);
}
void init_sa(sa *pthis){
   pthis->m_out=out;
}

int main(){
   sa so;
   init_sa(&so);
   so.m_out();
   return;
}

faku 发表于 2012-05-25 14:13

3楼的最后一段代码有问题吧
so.m_out();这个调用少了参数

NalaGinrut 发表于 2012-05-30 14:20

从原理上简单来说,编译时偷偷多传了一个指针进函数,该指针统一命名为this,没什么高深的东西
页: [1]
查看完整版本: 谁能给我简要说一下C++编译器的this指针式如何实现的?