c**********e 发帖数: 2007 | 1 class A {};
class B : A {};
void foo() { throw new B; }
Which of the following catch statements successfully catches the exception
foo throws?
a) catch (A* e) and catch (B* e)
b) catch (A e) and catch (B e)
c) catch (A* e)
d) catch (B* e)
e) catch (A e)
I have no answer. Help, please. |
s*********t 发帖数: 1663 | |
s*********t 发帖数: 1663 | 3 异常处理我一点不懂。
刚才试了一下,如果throw new A也不能catch B* |
s*****t 发帖数: 737 | 4 which is subtype and super type?
I think you might be wrong. Consider the following change, what is your
answer?
class A {};
class B : A {};
void foo() { throw new A; }
Which of the following catch statements successfully catches the exception
foo throws?
a) catch (A* e) and catch (B* e)
b) catch (A e) and catch (B e)
c) catch (A* e)
d) catch (B* e)
e) catch (A e) |
M******q 发帖数: 94 | 5 that's exactly why A* can catch new B(), i think |
c**********e 发帖数: 2007 | 6 I think it is d). You throw a pointer to B, then you need to catch it. Here
to catch a pointer to A is not enough. |
s*********t 发帖数: 1663 | 7 this makes perfect sense
Here
【在 c**********e 的大作中提到】 : I think it is d). You throw a pointer to B, then you need to catch it. Here : to catch a pointer to A is not enough.
|
x***y 发帖数: 633 | 8 Sorry, you are right...A* can only be caught by A* but B* can be caught by A
* and B*...
【在 s*****t 的大作中提到】 : which is subtype and super type? : I think you might be wrong. Consider the following change, what is your : answer? : class A {}; : class B : A {}; : void foo() { throw new A; } : Which of the following catch statements successfully catches the exception : foo throws? : a) catch (A* e) and catch (B* e) : b) catch (A e) and catch (B e)
|
x***y 发帖数: 633 | 9 Sorry, I made a mistacke, it should be a.
【在 s*********t 的大作中提到】 : why a is not right?
|
s*********t 发帖数: 1663 | 10 it's not a.
u just got the right answer but ur explaination wasn't
【在 x***y 的大作中提到】 : Sorry, I made a mistacke, it should be a.
|
x***y 发帖数: 633 | 11 Sorry, I made a mistake...A* can only be caught by A*, B* can be caught by A
* and B* as the thrown B* is also A* object....
【在 s*********t 的大作中提到】 : 异常处理我一点不懂。 : 刚才试了一下,如果throw new A也不能catch B*
|
x***y 发帖数: 633 | 12 It shoudl be a, my original answer is wrong....All the public inheritence
relations in c++ satisfy that the derived object is also a base object. here
, a B* is thrown, which is also a A*, that is why it can caught by both A*
and B*.
I verified in my programme and the throw new B can be caught by catch(A*).
【在 s*********t 的大作中提到】 : it's not a. : u just got the right answer but ur explaination wasn't
|
x***y 发帖数: 633 | 13 Sorry, I made a mistacke, you are right....
【在 M******q 的大作中提到】 : that's exactly why A* can catch new B(), i think
|