99. Don’t use invalid objects. Don’t use unsafe functions

Summary

Don’t use expired medicines: Both invalid objects and historical but unsafe functions wreak havoc on your program’s health.

Discussion

There are three major kinds of invalid objects:

Destroyed objects: Typical examples are automatic objects that have gone out of scope and deleted heap-based objects. After you call an object’s destructor, its lifetime is over and it is undefined and generally unsafe to do anything with it.

Semantically invalidated objects: Typical examples include dangling pointers to deleted objects (e.g., a pointer p after a delete p;) and invalidated iterators (e.g., a vector<T>::iterator i after an insertion at the beginning of the container the iterator ...

Get C++ Coding Standards: 101 Rules, Guidelines, and Best Practices 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.