tailieunhanh - HandBooks Professional Java-C-Scrip-SQL part 48

Tham khảo tài liệu 'handbooks professional java-c-scrip-sql part 48', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | void failure_is_error base1 p try some_other_class soc dynamic_cast some_other_class p Use soc catch std bad_cast e std cout n void failure_is_ok base1 p if some_other_class psoc dynamic_cast some_other_class p Use psoc In this example the pointer p is dereferenced15 and the target type of the conversion is a reference to some_other_class. This invokes the throwing version of dynamic_cast. The second part of the example uses the nonthrowing version by converting to a pointer type. Whether you see this as a clear and concise statement of the code s intent depends upon your experience. Veteran C programmers will understand the last example perfectly well. Will all of those reading the code be sufficiently familiar with the workings of dynamic_cast or is it possible that they ll be unaware of the fact that it works differently depending on whether the type being converted is a pointer or reference Will you or a maintenance programmer always remember to test for the null pointer Will a maintenance programmer realize that dereferencing the pointer is necessary to get the exception if the conversion fails Do you really want to write the same logic every time you need this behavior Sorry for this rhetoricits intent is to make it painfully obvious that polymorphic_cast makes a stronger clearer statement than dynamic_cast when a conversion failure should result in an exception. It either succeeds producing a valid pointer or it fails throwing an exception. Simple rules are easier to remember. 5 If the pointer p is null the example results in undefined behavior because it will dereference a null pointer. We haven t looked at how you can overload polymorphic_cast to account for unusual conversion needs but it should be noted that it s possible. When would you want to change the default behavior of a polymorphic cast One example is for handle body-classes where the rules for downcasting may be different from the default or should be disallowed altogether. Summary It is

TÀI LIỆU LIÊN QUAN
10    158    1
6    184    1
7    162    1
5    157    1
6    160    1
6    152    1
6    150    1
6    206    1
7    154    1