From 9efc2a70dcdb92b232c4c9f8317373b4534aafae Mon Sep 17 00:00:00 2001 From: Arun Saha Date: Sat, 18 Feb 2017 10:11:46 -0800 Subject: [PATCH 1/2] const member variable --- 03-Style.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/03-Style.md b/03-Style.md index b2c508f..e236628 100644 --- a/03-Style.md +++ b/03-Style.md @@ -277,11 +277,25 @@ private: // ... // ``` - Prefer {} initialization over alternatives unless you have a strong reason not to. Forgetting to initialize a member is a source of undefined behavior bugs which are often extremely hard to find. +If the member variable is not expected to change after the initialization, then mark it `const`. + +```cpp +class MyClass +{ +public: + MyClass(int t_value) + : m_value{t_value} + { + } + +private: + const int m_value{0}; +}; +``` ## Always Use Namespaces From 743c4ebbcb53d4a5c5aaa091fd185562b911c522 Mon Sep 17 00:00:00 2001 From: Arun Sah Date: Sat, 25 Feb 2017 07:35:23 -0800 Subject: [PATCH 2/2] Note on copy assignment --- 03-Style.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/03-Style.md b/03-Style.md index e236628..b8de498 100644 --- a/03-Style.md +++ b/03-Style.md @@ -297,6 +297,8 @@ private: }; ``` +Since a const member variable cannot be assigned a new value, such a class may not have a meaningful copy assignment operator. + ## Always Use Namespaces There is almost never a reason to declare an identifier in the global namespace. Instead, functions and classes should exist in an appropriately named namespace or in a class inside of a namespace. Identifiers which are placed in the global namespace risk conflicting with identifiers from other libraries (mostly C, which doesn't have namespaces).