C++类函数深入理解

最近在做Android 播放器的项目,native 需要用到C++,遇到一个有意思的问题,先mark下。

先看一段代码:

class A
{
public:
    int v;
    
public:
    A():v(0){}
    void print() const
    {
        printf("%s enter. this=%p\n", __func__, this);
    }
};

int main(int argc, const char * argv[])
{
    A* a = NULL;
    A* a1 = new A();
    A a2;
    
    //printf("a1->print = %p\n", &a1->print); // compile error
    //printf("a2.print = %p\n", &a2.print); // compile error

    //printf("A::print = %p\n", A::print); // compile error
    printf("&A::print = %p\n", &A::print);
    a->print();
    a->v = 1;
    
    return 0;
}
执行结果:

&A::print = 0x100000eb0
print enter. this=0x0

a->v = 1;  // 空指针

那么问题来了。一般情况下,a=NULL, 为空指针,访问a都是非法的。但是 

1. a->v = 1; --> crash (这里没有任何问题,a为空指针,并未实例化)

2. a->print(); --> 输出 "print enter. this=0x0"

这是一个有意思的问题。我们先来看上面有4行printf() ,前面2个printf() 都是编译错误。

//printf("a1->print = %p\n", &a1->print); // compile error
//printf("a2.print = %p\n", &a2.print); // compile error
只有最后一个 
printf("&A::print = %p\n", &A::print);
能正确输出类函数地址,从以上现象可以看出,类函数并不是和类实例绑定的,它是编译后固有的,存储在代码段,是只读的。执行

$nm ./test

可以看到其中一行信息:

0000000100000eb0 t __ZNK1A5printEv

这个正是 &A::print 的地址。其实C++的类函数本质上跟C函数一样,只是默认增加了this 参数。

例如上面的 

void A::print();
等价于

void __ZNK1A5printEv(A* this);
这样理解也就没有问题了。a->print(); 只是查找到 &A::print 在代码段的地址,调用该函数,并把 (a=NULL)作为参数传入,输出的 this=0x0,也是证实了这一点。

而对于 A::v 变量,它是和类实例绑定的,只有生成了实例,A::v 才有对应的内存空间。所以访问A::v 的时候,必须保证已经实例化。



 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值