Just some observations; not a critique of the answer. Show 3 more comments. It might help if you know little bit of internals Alternatively, A could have a conversion operator i. A::operator B. If B doesn't have such constructor, or A doesn't have a conversion operator, then you get compile time error. Gotcha : If you cast base pointer to derived pointer but if actual object is not really derived type then you don't get error.
You get bad pointer and very likely a segfault at runtime. Gotcha : Cast from Derived to Base or viceversa creates new copy! Unlike other casts, there is runtime overhead.
However if your intent is to modify object's non-key members then it should be ok. For example, you can load 4 bytes of float to 4 bytes of int to see how bits in float looks like. Obviously, if data is not correct for the type, you may get segfault. There is no runtime overhead for this cast. Andrew Truckle Shital Shah Shital Shah I added the conversion operator information, but there are a few other things that should be fixed as well and I don't feel that comfortable updating this too much.
Items are: 1. If you cast base pointer to derived pointer but if actual object is not really derived type then you don't get error. You get bad pointer and segfault at runtime.
You get UB which may result in a segfault at runtime if you're lucky. Dynamic casts can also be used in cross casting. Const casts can result in UB in some cases. Using mutable may be a better choice to implement logical constness.
Adrian you are correct in all count. The answer is written for folks at more or less beginner level and I didn't wanted to overwhelm them with all other complications that comes with mutable , cross casting etc. Shital Shah "Cast from Derived to Base or viceversa creates new copy! Show 2 more comments. Lii Serge Rogatch Serge Rogatch Here is the sample. Here developer clearly expressed he wanted to do this potentially dangerous operation. C-style cast can do such cast. Instantiating a class template with related types does not make the template instantiations also related.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 5 years, 11 months ago. Active 5 years, 11 months ago.
Viewed 9k times. Improve this question. Community Bot 1 1 1 silver badge. Narek Narek What is ActionLink and ILink? T must be a pointer, reference, or pointer-to-member type. It can be used to convert between pointer and integer types, between unrelated pointer types, between pointer-to-member types, and between pointer-to-function types. The following points describe the only ensured behavior:. It can be used for any type conversion that is allowed implicitly. In addition, any value can be cast to void , and any implicit conversion can be reversed if that cast would be legal as an old-style cast.
A pointer or reference to a class can actually point refer to any class derived from that class. Occasionally, it may be desirable to obtain a pointer to the fully derived class, or to some other subobject of the complete object. The dynamic cast provides this facility. Feedback will be sent to Microsoft: By pressing the submit button, your feedback will be used to improve Microsoft products and services. Privacy policy. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info.
0コメント