5.12 Confusing the Equality (==
) and Assignment (=
) Operators
There’s one error that C++ programmers, no matter how experienced, tend to make so frequently that we feel it requires a separate section. That error is accidentally swapping the operators ==
(equality) and =
(assignment). What makes this so damaging is that it ordinarily does not cause syntax errors—statements with these errors tend to compile correctly and the programs run to completion, often generating incorrect results through runtime logic errors. Some compilers issue a warning when =
is used in a context where ==
is expected.
Two aspects of C++ contribute to these problems. One is that any expression that produces a value can be used in the decision portion of any control statement ...
Get C++ How to Program, 10/e now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.