Created
April 19, 2015 20:24
-
-
Save dgodfrey206/33341ba2dfca3090462a to your computer and use it in GitHub Desktop.
More explicit type conversion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <new> | |
| struct Parent { }; | |
| struct Child : Parent { }; | |
| struct Tag { }; | |
| int main() | |
| { | |
| const Child c; | |
| Parent& p1 = const_cast<Parent&>(static_cast<Parent const&>(c)); | |
| Parent& p2 = (Parent&)c; // same as initializer for p1 | |
| const char buf[sizeof(Tag)]{}; | |
| new ((void*)buf) Tag(); | |
| Tag* tag1 = const_cast<Tag*>(reinterpret_cast<Tag const*>(buf)); | |
| Tag* tag2 = (Tag*)buf; // same as initializer for tag1 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The different C++ casts a C style cast will go through:
const_caststatic_caststatic_castfollowed byconst_castreinterpret_castreinterpret_castfollowed byconst_castIt looks for the first viable cast for the conversion.