-
July 18th, 2005, 12:21 AM
#1
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:
- Reference to object -> Can change the referenced object
Code:
foo instance;
foo& reference_to_instance = instance;
- Reference to constant object -> Can not change the referenced object
Code:
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
Code:
foo instance;
foo* pointer_to_instance = &instance;
- 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;
- Constant pointer -> Can change the object but no the address it is pointing to
Code:
foo instance;
foo* const constant_pointer_to_instance = &instance;
- 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;
Last edited by Andreas Masur; September 25th, 2005 at 11:30 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|