const-Correctness & Modern C++ Style
Course 2 built templates, containers, smart pointers, move semantics, exceptions, and lambdas. This closing chapter ties them together into the actual practice of writing idiomatic modern C++.
const-Correctness, In Full
cpp1-2 introduced const briefly; cpp1-8 covered const T& parameters. The remaining piece: a const member function promises not to modify the object, enforced by the compiler.
This is a real, propagating discipline — a const object can only call const-qualified member functions on itself.
The Rule of Three
Revisiting cpp1-5's own preview directly: if a class defines any one of a destructor, a copy constructor, or a copy assignment operator, it almost certainly needs all three. It's managing a resource manually, and the compiler's own default-generated versions of the other two would perform a shallow copy — two objects both believing they own the same resource, leading straight to c2-3's own double-free bug class.
The Rule of Five
C++11 extended it: the move constructor and move assignment operator (cpp2-5's own material) join the same rule. Writing any one of the five is a real signal you likely need to think through all five.
The Rule of Zero
The modern, actually-recommended alternative: don't manage raw resources directly at all. Use RAII wrappers — smart pointers (cpp2-4), STL containers (cpp2-2) — for every member, and let the compiler's default-generated destructor, copy, and move operations work correctly automatically, since each member's own RAII type already does the right thing on its own. A genuine, real best practice: the Rule of Five exists to be understood; the Rule of Zero exists to be practiced. Most well-written modern C++ classes need none of the five written by hand.
auto and Range-Based for Loops, Revisited
cpp1-2's auto and cpp2-3's range-based for, named together now as part of "modern C++ style" — alongside the Rule of Zero, smart pointers, and RAII, this is what a well-written modern codebase actually looks like, in sharp contrast to older, pre-C++11 "C with Classes" style still using raw new/delete and hand-written Rule-of-Three classes everywhere.
Closing Course 2
Templates → STL containers → iterators/algorithms → smart pointers → move semantics → exceptions → lambdas — now const-correctness and the Rule of Zero tie the whole course together into a genuine practice, not just a list of features. Course 3 goes deeper still: multiple inheritance, templates in more depth, the Rule of Five's full mechanics, concurrency, C++-specific undefined behavior, and a real capstone.
| Concept | Old "C with Classes" | Modern C++ |
|---|---|---|
| Resource management | raw new/delete, hand-written Rule of Three/Five | RAII wrappers, Rule of Zero |
| Containers | hand-rolled or raw arrays | std::vector/std::map and the rest of the STL |
| Type declarations | always spelled out explicitly | auto where it aids readability |
c2-3's own bug catalog.
Coding Challenges
Write a class with a const member function that computes and returns a value from the object's own data without modifying it. Then attempt to call a non-const member function on a const instance of that class, and report the compile error.
📄 View solutionWrite a class that follows the Rule of Zero by holding a std::unique_ptr
Write a class managing a raw new'd int* member with a hand-written destructor, but deliberately no copy constructor or copy assignment operator. Copy an instance of it, let both copies go out of scope, and explain precisely what goes wrong.
📄 View solutionChapter 8 Quick Reference — Course 2 Complete
- const member functions — a compiler-enforced promise not to modify the object
- Rule of Three — destructor, copy constructor, copy assignment: write one, likely need all three
- Rule of Five — the Rule of Three plus move constructor and move assignment (C++11)
- Rule of Zero — the modern practice: hold RAII members only, let the compiler generate everything correctly
- Modern C++ style: RAII, smart pointers, the STL,
auto, range-basedfor— vs. older raw new/delete "C with Classes" - Course 2 complete. Course 3 covers multiple inheritance, deeper templates, the Rule of Five in full, modern concurrency, C++-specific UB, and a real capstone