This topic has been archived. It cannot be replied.
-
工作学习 / 科技领域杂谈 / 典型C++题一道,请写出输出结果。
-liquid(豆泡松果 之 松果豆泡);
2010-5-22
{919}
(#6084757@0)
-
有两个小小的bug:C++ cast漏了类型,打印指针的时候字符串里变量名。打印加换行好看点。把父类指针cast到子类指针得非常小心。最好是不需要cast,而是依赖polymorphism。在c-tor和d-tor里调用virtual function也是搞笑。当然最有破坏力的是pB1->m_b = 1
-wangqingshui(忘情水);
2010-5-23
{974}
(#6085350@0)
-
端个小板凳,带上笔记本,等高人评点。。。
-eagle_no1(瞎起哄);
2010-5-23
(#6085595@0)
-
我随便说说这是一道比较有趣的题目。主要考察方向大概有以下几点:
1,关于从析构函数里面抛出异常,这是允许的。但是不允许从构造函数抛。
2,关于DOWN CAST,这几种都是可以的。dynamic_cast会在你CALL了一个A中不存在的方法的时候抛出std::bad_cast异常。但是这里没有CALL,所以没有。
3,执行->foo的时候,因为A当中也有这个方法,是正常执行。
4,给mB给值的时候,超界了, sizeof(B)和sizeof(A)在这里是不同的。所以当你退出程序的时候,出错。STACK崩溃。
-oscargee(oscargee);
2010-6-23
{422}
(#6143170@0)
-
Q1:What really happens when you call virtual function and throw exception in dtors? Q2:What's the difference between those castings? Q3: What exactly will "pB1->m_b = 1;" do here? Thanks wangqingshui for formatting and correcting variable names.
-liquid(豆泡松果 之 松果豆泡);
2010-5-24
(#6086597@0)
-
A1: calling virtual function in dtor I believe the function is considered non-virtual because the object of the derived class part has been destructed already.
-wangqingshui(忘情水);
2010-5-25
(#6087476@0)
-
Yes, if virtual function was called directly from ctor/dtor, it is not considered as virtual; however if it is called indirectly (e.g. through another non-virtual function), it will be considered as a virtual function call and vtable will be used.
-liquid(豆泡松果 之 松果豆泡);
2010-5-25
(#6087534@0)
-
A1: Throwing exception from dtor or from functions called in dtor may cause 2 parallel unhandled exception and abort the application.
-wangqingshui(忘情水);
2010-5-25
(#6087478@0)
-
The try/catch will catch the C++ exception, and the exception didn't even stop stack unwinding.
-liquid(豆泡松果 之 松果豆泡);
2010-5-25
(#6087544@0)
-
Q3: it is kind of similar to the situation that someone intrusively peed in his neighbour's yard, possibly burnt a precious flower, or maybe peed into empty area without any actual damage.
-wangqingshui(忘情水);
2010-5-25
(#6087497@0)
-
Normally it corrupts stack depending how far it goes. With VC8, it runs without problem with release build and error of stack corruption in debug build.
-liquid(豆泡松果 之 松果豆泡);
2010-5-25
(#6087548@0)
-
钻牛角尖的时间用在任何地方得到的回报都要比钻牛角尖高。
-wincity(toad 会英文的红卫兵);
2010-5-25
(#6087525@0)
-
拿一堆code,问人家结果,比钻牛角尖更浪费时间。
-wincity(toad 会英文的红卫兵);
2010-5-25
(#6087527@0)