C++20 sections · 1024 units
Open in Course

Problem: Const Correctness

Using const references

Write void printPerson(const string& name, const int& age) that prints a formatted message. The const references prevent accidental modification while avoiding string copies. Inside, you can read name and age but can't assign to them.

Try name = "other" and see the compiler error. This protection helps catch bugs where you modify parameters you meant to only read. I make parameters const by default in my own code.

If I need to modify something later, I remove const. This approach documents intent and catches mistakes early.