Tutorial by Examples

const correctness is the practice of designing code so that only code that needs to modify an instance is able to modify an instance (i.e. has write access), and conversely, that any code that doesn't need to modify an instance is unable to do so (i.e. only has read access). This prevents the insta...
In a const-correct class, all member functions which don't change logical state have this cv-qualified as const, indicating that they don't modify the object (apart from any mutable fields, which can freely be modified even in const instances); if a const cv-qualified function returns a reference, t...
In a const-correct function, all passed-by-reference parameters are marked as const unless the function directly or indirectly modifies them, preventing the programmer from inadvertently changing something they didn't mean to change. This allows the function to take both const and non-cv-qualified ...
One of the more useful things about const correctness is that it serves as a way of documenting code, providing certain guarantees to the programmer and other users. These guarantees are enforced by the compiler due to constness, with a lack of constness in turn indicating that code doesn't provide...

Page 1 of 1