const member variable

This commit is contained in:
Arun Saha
2017-02-18 10:11:46 -08:00
parent eea3d25f0c
commit 9efc2a70dc

View File

@@ -277,11 +277,25 @@ private:
// ... // // ... //
``` ```
Prefer {} initialization over alternatives unless you have a strong reason not to. 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. 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 ## Always Use Namespaces