Skip to Content
Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions
book

Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions

by Herb Sutter
November 1999
Intermediate to advanced
240 pages
5h 22m
English
Addison-Wesley Professional
Content preview from Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions

Chapter 43. Const-Correctness

Difficulty: 6

const is a powerful tool for writing safer code. Use const as much as possible, but no more. Here are some obvious and not-so-obvious places where const should—or shouldn't—be used.

Don't comment on or change the structure of this program; it's contrived and condensed for illustration only. Just add or remove const (including minor variants and related keywords) wherever appropriate.

Bonus question: In what places are the program's results undefined/uncompilable due to const errors?

 class Polygon { public: Polygon() : area_(-1) {} void AddPoint( const Point pt ) { InvalidateArea(); points_.push_back(pt); } Point GetPoint( const int i ) { return points_[i]; } int GetNumPoints() { return points_.size(); } ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Exceptional C++ Style 40 New Engineering Puzzles, Programming Problems, and Solutions

Exceptional C++ Style 40 New Engineering Puzzles, Programming Problems, and Solutions

Herb Sutter

Publisher Resources

ISBN: 0201615622Purchase book