-
Notifications
You must be signed in to change notification settings - Fork 5
create const_cast.md // dynamic_cast.md #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SoWeBegin
wants to merge
8
commits into
eisenwave:master
Choose a base branch
from
SoWeBegin:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dda1803
Create const_cast.md
SoWeBegin 818e754
Update const_cast.md
SoWeBegin f95f3a5
Update const_cast.md
SoWeBegin 32a7b37
Update const_cast.md
SoWeBegin 39b9c7e
Create dynamic_cast.md
SoWeBegin 5b5a57c
Update dynamic_cast.md
SoWeBegin c02e34c
Update const_cast.md
SoWeBegin 2dfe142
Update dynamic_cast.md
SoWeBegin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| ## `const_cast` conversion | ||
| `const_cast` is an explicit cast operator that is mainly used to remove (or add) constness from/to a pointer or reference. | ||
| This operator can only remove or add the constness to **what a pointer (or reference) points to**, and *never* the constness of the object itself: otherwise, we would break the immutability of the object. | ||
|
|
||
| ## Examples | ||
| - Remove constness: | ||
| ```cpp | ||
| int main() { | ||
| const int i = 0; | ||
| const int* const pointer = &i; | ||
| int* x = const_cast<int*>(pointer); // Remove const from | ||
| // what the pointer points to -- valid | ||
| *const_cast<int*>(pointer) = 2000; // No compile time error, but undefined | ||
| // behavior: we cannot mutate the object x points to, because it is originally const! | ||
| } | ||
| ``` | ||
| - Add constness: | ||
| ```cpp | ||
| int main() { | ||
| int i = 0; | ||
| int* pointer = &i; | ||
| *const_cast<const int*>(pointer) = 5; // Doesn't compile | ||
| // since we added a const qualifier: we now cannot mutate this object's value! | ||
| } | ||
| ``` | ||
|
|
||
| ## `const_cas`t might be dangerous to use | ||
| Unless you have a very good reason to, *you should avoid `const_cast`*. It can easily lead | ||
| to undefined behavior and it's very easy to break the constness-related best practices while using it. | ||
|
|
||
| ## Also See | ||
| [cppreference: const_cast](https://en.cppreference.com/w/cpp/language/const_cast) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add more links in both articles. Stackoverflow posts, CppCoreGuidelines recommendations for dynamic-cast and const-cast, etc. |
||
| [how to use const_cast?](https://stackoverflow.com/questions/19554841/how-to-use-const-cast) | ||
| [is const_cast safe?](https://stackoverflow.com/questions/357600/is-const-cast-safe) | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| ## `dynamic_cast` conversion | ||
| `dynamic_cast` is an explicit cast operator that is often used when working with inheritance hierarchies, to convert pointers | ||
| (and references) from the derived class to the base class (upcasting) and the opposite (downcasting). | ||
| This operator performs a runtime check to test whether the conversion can be done: it returns a null pointer on failure. | ||
| This implies a little overhead when using it. | ||
|
|
||
| ## Useful conversions | ||
| `dynamic_cast` is mostly used when we need to downcast a base class to its derived class. | ||
| It is important to check whether the cast was successful: we can test whether the pointer returned by | ||
| `dynamic_cast` is nullptr, and if it is, the conversion failed. | ||
| ```cpp | ||
| int main() { | ||
| Base* base = new Derived; | ||
| if (Derived* derived = dynamic_cast<Derived*>(base); derived != nullptr) /* Check that the conversion did not fail */ { | ||
| derived->foo(); | ||
| } | ||
| // Note: If base didn't point to a derived object, the if statement would never execute because derived would be null | ||
| delete base; | ||
| } | ||
| ``` | ||
|
|
||
| ## Notes | ||
| You can downcast with `static_cast` too, but no runtime check is performed. | ||
| Unless you can prove that the conversion will not fail, use use dymamic_cast in that it's safer and will avoid | ||
| unwanted issues. | ||
|
|
||
|
|
||
| ## See Also | ||
| - [cppreference: dynamic_cast](https://en.cppreference.com/w/cpp/language/dynamic_cast) | ||
| - [how to down_cast correctly](https://stackoverflow.com/questions/52556957/how-to-use-dynamic-cast-to-downcast-correctly) | ||
| - [is dynamic_cast considered bad design?](https://stackoverflow.com/questions/48612271/use-case-of-dynamic-cast) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reduce LOC by removing
int main(), we don't need a whole program to demonstrate how it works