Skip to content

Commit b2ac26c

Browse files
authored
Merge pull request #167 from UCL/mm_updates2025
Explain initialiser list
2 parents 213ec58 + da65a4f commit b2ac26c

1 file changed

Lines changed: 2 additions & 0 deletions

File tree

02cpp1/sec03ObjectOrientedProgramming.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ class Ball
210210
};
211211
```
212212
213+
- The syntax `: position(p), radius(r), mass(m)` after the constructor signature is called an initialiser list. This can be used to initialise member variable with simple expressions. It's very useful when just assigning constructor arguments to member variables.
214+
213215
Now we can even make our code **more flexible without sacrificing safety**. Let's say the ball can change _mass_ or _radius_. We can't just make these variables public and change them independently, because then the _density_ will no longer be consistent with the new mass / radius. We need to add **setter** functions which **maintain the integrity of the object**:
214216
```cpp
215217
class Ball

0 commit comments

Comments
 (0)