Accessing const objects

You have seen many examples so far of using const, and perhaps the most frequent is when it is applied to a reference as a function parameter to indicate to the compiler that the function only has read-only access to the object. Such a const reference is used so that objects are passed by reference to avoid the overhead of the copying that would occur if the object were passed by value. Methods on a class can access the object data members and, potentially, can change them, so if you pass an object through a const reference the compiler will only allow the reference to call methods that do not change the object. The point class defined earlier had two accessors to access the data in the class:

    class point     {  double ...

Get Beginning C++ Programming 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.