liveBook · Manning

Content shadowing

As explained in 100 C++ Mistakes and How to Avoid Them

Content shadowing is a variable-shadowing error in which code uses an inner variable when the corresponding outer variable was intended.

Content shadowing happens when an inner variable hides an outer variable and code uses the inner name by mistake. The result may be a typo that compiles but produces the wrong behavior. section 9.1

A setter that changes nothing

In Circle, the parameter rad, member rad, and global rad share a name. The statement rad = rad assigns the parameter to itself, while sum() adds the member twice instead of combining member and global state. Use clearer names or explicit qualification, such as this->radius and global::radius.

How 100 C++ Mistakes and How to Avoid Them covers it

How deep it goes
Defines the error, traces it through listings, and stops short of broader redesign beyond renaming and qualification.
Examples
Uses Circle radius calculations, a self-assignment setter, and namespace-qualified corrections as concrete examples.
What it assumes
Assumes basic C++ syntax while making scope confusion concrete through classes, members, parameters, and namespaces.
What you can do after
Diagnose/debug with it by spotting hidden variables and correcting names, this qualification, or namespace qualification.
How it connects
Connects variable shadowing with scope resolution, namespaces, naming conventions, and legacy-code maintenance.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 C++ Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.