CS 105 (C++)

Discussion 14
C++: Other Things You Should Know


How have we previously seen the const keyword used (in both C and C++)?

const int size = 10;  

But, the C++ textbook has this to say about const:

"Unfortunately for the C++ beginner, the meaning of const in C++ differs slightly from that of C.  const can also be quite subtle and complicated to use correctly in C++ ... For these reasons, we leave const to a more advanced book."

So we won't consider all of the details, but let's take a brief look at a very common and useful application of const in C++:

What does const mean in this copy constructor declaration?  (Note: This use of const is probably typical for a copy constructor.)

NewClass(const NewClass& source);

It means that the source object may not be modified within this function.  

And, what does it mean in the declaration of this member function?

int getValue() const;

It indicates that this function can be safely called on a const instance of this class (like source in the copy constructor above) without modifying the object.