some questions about const

This is a discussion on some questions about const within the C++ Programming forums, part of the General Programming Boards category; The const keyword can be placed at different places. Code: const int *ptr3 = 0; int const *ptr4 = 0; ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    88

    some questions about const

    The const keyword can be placed at different places.
    Code:
    const int *ptr3 = 0;
    int const *ptr4 = 0;
    Code:
    const int a = 5; //can not be changed
    int const b = 6;//can also not be changed
    What`s the difference?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    In this case, your preference is the only difference.

    If you would want a const pointer to int instead of a pointer to const int, placement does matter.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    The only time it matters is with pointers.
    If the const comes before the * as in: const char* string; then that means the string cannot be changed, but the pointer can be changed.
    If the const comes after the * as in: char* const string; then that means the pointer to string cannot be changed, but the string itself can be changed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 11:17 AM
  2. Classes & Collections
    By Max_Payne in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2007, 01:06 PM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 02:03 AM
  4. Memory leak - need help finding
    By ChadJohnson in forum C++ Programming
    Replies: 8
    Last Post: 04-06-2005, 08:26 PM
  5. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 08:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21