Need help with const pointers
Lately I've been teaching myself C++ through books and the net and it's been going very smoothly. I'm up to Ch.9 on this site which deals with references & pointers. Currently, I'm at "Passing a Const Pointer" but I can't quite understand what they're saying completely and I'd hate to move on without knowing the material, so I'm asking for a little help.
Just under the "Passing a Const Pointer", they mention the following (refers to figure 9.10):
I'm wondering why this is considered dangerous, and why exactly is FunctionTwo() not allowed to change the object it is passed. I'm a bit confused by this since in the instructions leading up to this, we were passing variables by reference into similar functions, adjusting their values, and displaying the results in main. How is this different?
Although passing a pointer to FunctionTwo() is more efficient, it is dangerous. FunctionTwo() is not allowed to change the SimpleCat object it is passed, yet it is given the address of the SimpleCat. This seriously exposes the object to change and defeats the protection offered in passing by value.
I'm also a tad confused by the prototype we use for FunctionTwo() in figure 9.11:
const SimpleCat * const FunctionTwo (const SimpleCat * const theCat);
Is it possible anyone can break this down to make it easier to understand? :( Sorry to bother with "newbie" questions, but I'm committed to learning this language inside and out but it's kinda hard without a teacher.
On line 46, itsAge is set using the accessor SetAge, and the result is printed on line 47. FunctionOne is not used in this program, but FunctionTwo() is called. FunctionTwo() has changed slightly; the parameter and return value are now declared, on line 36, to take a constant pointer to a constant object and to return a constant pointer to a constant object.