Q: What are the differences between constant objects?
A: To make any object constant, one uses the keyword 'const'. However, depending on the actual place you insert this keyword, it can have different meanings for the associated object:
- Reference to object -> Can change the referenced object
foo instance;
foo& reference_to_instance = instance;
- Reference to constant object -> Can not change the referenced object
foo instance;
const foo& constant_reference_to_instance = instance;
- Pointer to object -> Can change the adress it is pointing to as well as the object
foo instance;
foo* pointer_to_instance = &instance;
- Pointer to constant object -> Can change the adress it is pointing to but not the object
foo instance;
const foo* pointer_to_constant_instance = &instance;
- Constant pointer -> Can change the object but no the address it is pointing to
foo instance;
foo* const constant_pointer_to_instance = &instance;
- Constant pointer to constant object -> Can neither change the address nor the object
foo instance;
const foo* const constant_pointer_to_constant_instance = &instance;
Note: Most of the material in this article is taken from codeguru.com
Recommended Reading :