Skip to content

Commit 9efc2a7

Browse files
committed
const member variable
1 parent eea3d25 commit 9efc2a7

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

03-Style.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,25 @@ private:
277277
// ... //
278278
```
279279
280-
281280
Prefer {} initialization over alternatives unless you have a strong reason not to.
282281
283282
Forgetting to initialize a member is a source of undefined behavior bugs which are often extremely hard to find.
284283
284+
If the member variable is not expected to change after the initialization, then mark it `const`.
285+
286+
```cpp
287+
class MyClass
288+
{
289+
public:
290+
MyClass(int t_value)
291+
: m_value{t_value}
292+
{
293+
}
294+
295+
private:
296+
const int m_value{0};
297+
};
298+
```
285299

286300
## Always Use Namespaces
287301

0 commit comments

Comments
 (0)