From d9a035cbc4052067305f8bf09e3f2c00a16b53c9 Mon Sep 17 00:00:00 2001 From: rob100 Date: Fri, 5 Jun 2015 11:06:28 +0200 Subject: [PATCH] Prefer C++-style cast Use C++-style cast instead of C-style cast --- 04-Considering_Safety.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/04-Considering_Safety.md b/04-Considering_Safety.md index 3dc0eac..2eb760c 100644 --- a/04-Considering_Safety.md +++ b/04-Considering_Safety.md @@ -67,6 +67,19 @@ Exceptions cannot be ignored. Return values, such as using `boost::optional`, ca Stroustrup, the original designer of C++, [makes this point](http://www.stroustrup.com/bs_faq2.html#exceptions-why) much better than I ever could. +## Use C++-style cast instead of C-style cast +Use the C++-style cast(static\_cast<>, dynamic\_cast<> ...) instead of the C-style cast. The C++-style cast allows more compiler checks and is considerable safer. + +```cpp +// Bad Idea +double x = getX(); +int i = (int) x; + +// Good Idea +int i = static_cast(x); +``` +Additionaly the C++ cast style is more visible and has the possiblity to search for. + ## Additional Resources [How to Prevent The Next Heartbleed](http://www.dwheeler.com/essays/heartbleed.html) by David Wheeler is a good analysis of the current state of code safety and how to ensure safe code.