C++ : 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:

  1. Reference to object -> Can change the referenced object
    foo instance;
    foo& reference_to_instance = instance;
  2. Reference to constant object -> Can not change the referenced object
    foo instance;
    const foo& constant_reference_to_instance = instance;
  3. Pointer to object -> Can change the adress it is pointing to as well as the object
    foo instance;
    foo* pointer_to_instance = &instance;
  4. 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;
  5. Constant pointer -> Can change the object but no the address it is pointing to
    foo instance;
    foo* const constant_pointer_to_instance = &instance;
  6. 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 :





Loan - Cheap Gas - Mortgage - Credit Card Consolidation