m******e 发帖数: 481 | 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;
} |
m******e 发帖数: 481 | 2 Correct the grammer problem from typing in OP
Interviewer did say there is problem |
l*****a 发帖数: 14598 | 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;} : };
|
m******e 发帖数: 481 | 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++
|
l*****a 发帖数: 14598 | 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
|
m*****a 发帖数: 636 | 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
|
m******e 发帖数: 481 | 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
|
l*****a 发帖数: 14598 | 8 could u read more effective c++ Item13?
【在 m******e 的大作中提到】 : "the 4 basic std expections are not a pointer : What does this mean? : Thanks
|
p*********t 发帖数: 2690 | 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
|
p*********t 发帖数: 2690 | 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
|