下面两个应该是如同高考时候数学公式一样,想都不想都能说出来的。
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
...{
public: int 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继承
}以前看书也是有个印象,但是仍旧无法十分确认,
学无止境,时而复习之,不亦乐乎。
本文探讨了C++中引用重解析的概念及其使用场景,包括如何通过类型转换调用不同基类的方法。同时,文章还讨论了结构体与类在继承时成员变量访问权限的不同之处。
281

被折叠的 条评论
为什么被折叠?



