引用重解析和struct继承

来源:互联网 发布:网站源码下载 php 编辑:程序博客网 时间:2024/05/22 05:20

下面两个应该是如同高考时候数学公式一样,想都不想都能说出来的。

1,引用重解析,引用和指针一样,可以告诉程序重新以别的方式看待一块 内存,指针是最常用的。

void func(TypeA* _arg);
void caller()
{
      TypeB* b = new TypeB;
      func((TypeA)b);
}

类似的,引用可以有类似用法:

void func(TypeA& _arg);
void caller()
{
    TypeB b;
    func((TypeA
&)b);
}

恩,写在这里,不是因为这是个难的东西,只是不常见,我刚刚遇到,想到是*(TypeA*)(&b);来做,毫无疑问,学问不扎实导致这种sb想法。

2,另外 struct继承来的数据的私有性问题:

class ref
{
    
publicint num;
}
;

struct ref_kid : ref
{
}
;

class ref_class : ref
{
}
;

void caller()
{
    ref_kid _ref_kid;
    ref_class _ref_class;
    cout
<<_ref_kid.num<<endl; //right, struct 默认是public继承
  cout<<_ref_class.num<<endl; //wrong, class 默认是protected继承
}

以前看书也是有个印象,但是仍旧无法十分确认,

学无止境,时而复习之,不亦乐乎。