avatar
帮看看这段code# JobHunting - 待字闺中
m*e
1
这段code有什么问题?
Thanks
struct Exception
{
virtual int Type(){ return 0;}
};
struct ExceptionChild:Exception
{
virtual int Type(){ return 1;}
};
int _tmain(int argc, _TCHAR* argv[])
{
try
{
throw ExceptionChild();
}
catch(ExceptionChild e)
{
printf("Type value %d", e.Type());
}
return 0;
}
avatar
m*e
2
Correct the grammer problem from typing in OP
Interviewer did say there is problem
avatar
l*a
3
you should use catch by reference
for details, please read more effective C++

【在 m******e 的大作中提到】
: 这段code有什么问题?
: Thanks
: struct Exception
: {
: virtual int Type(){ return 0;}
: };
: struct ExceptionChild:Exception
: {
: virtual int Type(){ return 1;}
: };

avatar
m*e
4
Thanks. find it
The standard practice for exceptions in C++ is ...
Throw by value, catch by reference
Catching by value is problematic in the face of inheritance hierarchies.
Suppose for your example that there is another type MyException which
inherits from CustomException and overrides items like an error code. If a
MyException type was thrown your catch block would cause it to be converted
to a CustomException instance which would cause the error code to change.
References

【在 l*****a 的大作中提到】
: you should use catch by reference
: for details, please read more effective C++

avatar
l*a
5
catch by value will generate a local copy, will cause type slice
catch by pointer may need to think whom to delete the pointer
also the 4 basic std expections are not a pointer

converted

【在 m******e 的大作中提到】
: Thanks. find it
: The standard practice for exceptions in C++ is ...
: Throw by value, catch by reference
: Catching by value is problematic in the face of inheritance hierarchies.
: Suppose for your example that there is another type MyException which
: inherits from CustomException and overrides items like an error code. If a
: MyException type was thrown your catch block would cause it to be converted
: to a CustomException instance which would cause the error code to change.
: References

avatar
m*a
6
catch的已经是ExceptionChild了,
也还用reference嘛?

Thanks. find it
The standard practice for exceptions in C++ is ...
Throw by value, catch by reference
Catching by value is problematic in the face of inheritance hierarchies.
Suppose for your example that there is another type MyException which
inherits from CustomException and overrides items like an error code. If a
MyException type was thrown your catch block would cause it to be converted
to a CustomException instance which would cause the error code to change.
References

【在 m******e 的大作中提到】
: Thanks. find it
: The standard practice for exceptions in C++ is ...
: Throw by value, catch by reference
: Catching by value is problematic in the face of inheritance hierarchies.
: Suppose for your example that there is another type MyException which
: inherits from CustomException and overrides items like an error code. If a
: MyException type was thrown your catch block would cause it to be converted
: to a CustomException instance which would cause the error code to change.
: References

avatar
m*e
7
"the 4 basic std expections are not a pointer
What does this mean?
Thanks

【在 l*****a 的大作中提到】
: catch by value will generate a local copy, will cause type slice
: catch by pointer may need to think whom to delete the pointer
: also the 4 basic std expections are not a pointer
:
: converted

avatar
l*a
8
could u read more effective c++ Item13?

【在 m******e 的大作中提到】
: "the 4 basic std expections are not a pointer
: What does this mean?
: Thanks

avatar
p*t
9
throw MyException();
catch (CustomException& x)
这样不会引起late binding吗?

converted

【在 m******e 的大作中提到】
: Thanks. find it
: The standard practice for exceptions in C++ is ...
: Throw by value, catch by reference
: Catching by value is problematic in the face of inheritance hierarchies.
: Suppose for your example that there is another type MyException which
: inherits from CustomException and overrides items like an error code. If a
: MyException type was thrown your catch block would cause it to be converted
: to a CustomException instance which would cause the error code to change.
: References

avatar
p*t
10
如果 catch()里面已经是exceptionChild的话,不用reference也行,多用一次copy
constructor而已.

converted

【在 m*****a 的大作中提到】
: catch的已经是ExceptionChild了,
: 也还用reference嘛?
:
: Thanks. find it
: The standard practice for exceptions in C++ is ...
: Throw by value, catch by reference
: Catching by value is problematic in the face of inheritance hierarchies.
: Suppose for your example that there is another type MyException which
: inherits from CustomException and overrides items like an error code. If a
: MyException type was thrown your catch block would cause it to be converted

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。