面试题 - reinterpret_cast
来源:互联网 发布:零基础学日语 知乎 编辑:程序博客网 时间:2024/06/07 20:27
最近参加了一些面试,总是能遇见一些BT的题目,分享一下吧。
题目:class C多重继承于class A和Class B,如果想取消对B的继承,如何修改已有代码。条件是A和B不能修改。
我想,既然取消了继承,那用组合就行了。
然后,就是无理取闹的过程了,问用到了B的指针指向了C的对象,这时怎么办。
1: B* pb = new C;
肯定是编译不过啦,让解决以后的编译问题,还提示用cast。
cast是那么好用的吗,比如下面的例子
1: #include
2:
3: using namespace std;
4:
5: class B{
6: public:
7: void f()
8: {
9: cout << "B::f()" << endl;
10: }
11: };
12:
13: class C{
14: public:
15: void g()
16: {
17: cout << "C::g()" << endl;
18: }
19: };
20:
21: int main()
22: {
23: B *b = new B;
24: (reinterpret_cast(b))->g();
25:
26: system("pause");
27: return 0;
28: }
如果两个类里面又有了virtual函数,就完全不是一回事了。
现在的结果是C::g(),而声名了virtual函数后,就会指向vptr,那样就会显示B::f()。
也许题目的目的是考察reinterpret_cast吧,那也够偏的了,还有考察mutable的,nnnd。
- 面试题 - reinterpret_cast
- reinterpret_cast
- reinterpret_cast
- reinterpret_cast
- reinterpret_cast
- reinterpret_cast
- reinterpret_cast
- reinterpret_cast
- reinterpret_cast
- reinterpret_cast
- reinterpret_cast
- reinterpret_cast
- reinterpret_cast
- reinterpret_cast
- reinterpret_cast
- reinterpret_cast
- reinterpret_cast
- reinterpret_cast
- 好的软件人员一生必看的六十本书
- Linux下PHP支持MSSQL(SQL Server2000)
- Ajax实例
- Linux简单安装Mysql+Apach+PHP+phpMyAdmin
- 完美的拼接技术。。。。
- 面试题 - reinterpret_cast
- 2237: Hero Ranklist
- 关于IC卡和火车票的一点想法,票贩子不要骂我
- g++编译错误:expected unqualified-id before ‘using’
- linux进程调度相关问题记录
- SQL Server的链接服务器技术小结
- 第四节 GROUP BY 和 HAVING 子句
- 配置Firefox ScribeFire
- SQL Server中链接服务器将替代远程服务器