#122 – Static member variables

Consider a class with member variables. Each time an instance is made, each object gets a duplicate of the member variables. A member variable can be assigned static. This means all class objects share (reference) the same member. This allows…

#120 – The third type of class member

The problem Consider this program: This works, but DoorState exists outside the Door class although it’s designed to be used with Door. If only there was a way to embed Door inside the class, thereby making the relationship explicit. Nested types So far we’ve covered…

#119 – The hidden this pointer

The problem Consider this class: Both objects call the same member function: setVolume() But this call modifies music: music.setVolume(50); and this call modifies effects: effects.setVolume(20); How does C++ know which object m_volume belongs to? The answer is the hidden this pointer. What this means Inside a non-static member…

#116 – Crash course on the copy constructor

Copying an object Consider a simple Triangle class: This line constructs copy from another Triangle: Triangle copy { original }; That calls the copy constructor. Since we did not define a copy constructor, the compiler generates one for us. The compiler-generated copy constructor performs memberwise…