From f6b955a3e95bea7d9aeceb0f50a2566cfa6d692f Mon Sep 17 00:00:00 2001 From: Vladislav Yaroslavlev Date: Sun, 23 Aug 2015 02:06:03 +0300 Subject: [PATCH] Update 04-Considering_Safety.md Passing and returning simple types --- 04-Considering_Safety.md | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/04-Considering_Safety.md b/04-Considering_Safety.md index 8ca1849..c1b7c80 100644 --- a/04-Considering_Safety.md +++ b/04-Considering_Safety.md @@ -49,6 +49,52 @@ You don't want to have to pay a cost for copying the data when you don't need to See also this discussion for more information: https://github.com/lefticus/cppbestpractices/issues/21 +### Do not pass and return simple types by const ref + +```cpp +// Very Bad Idea +class MyClass +{ +public: + explicit MyClass(const int& t_int_value) + : m_int_value(t_int_value) + { + } + + const int& get_int_value() const + { + return m_int_value; + } + +private: + int m_int_value; +} +``` + +Instead, pass and return simple types by value. If you plan not to change passed value, declare them as `const`, but not `const` refs: + +```cpp +// Good Idea +class MyClass +{ +public: + explicit MyClass(const int t_int_value) + : m_int_value(t_int_value) + { + } + + int get_int_value() const + { + return m_int_value; + } + +private: + int m_int_value; +} +``` + +Why? Because passing and returning by reference leads to pointer operations instead by much more faster passing values in processor registers. + ## Avoid Raw Memory Access Raw memory access, allocation and deallocation, are difficult to get correct in C++ without [risking memory errors and leaks](http://blog2.emptycrate.com/content/nobody-understands-c-part-6-are-you-still-using-pointers). C++11 provides tools to avoid these problems.