C++ Memory Management: What are the differences between constant objects?
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:
<br>
- Reference to object -> Can change the referenced object
Code:
foo instance;
foo& reference_to_instance = instance;
<br> - Reference to constant object -> Can not change the referenced object
Code:
foo instance;
const foo& constant_reference_to_instance = instance;
<br> - Pointer to object -> Can change the adress it is pointing to as well as the object
Code:
foo instance;
foo* pointer_to_instance = &instance;
<br> - Pointer to constant object -> Can change the adress it is pointing to but not the object
Code:
foo instance;
const foo* pointer_to_constant_instance = &instance;
<br> - Constant pointer -> Can change the object but no the address it is pointing to
Code:
foo instance;
foo* const constant_pointer_to_instance = &instance;
<br> - Constant pointer to constant object -> Can neither change the address nor the object
Code:
foo instance;
const foo* const constant_pointer_to_constant_instance = &instance;
<br><br>