Thursday, November 25, 2010

Const

The folloiwng compiles:

      int                g_int        = 1;
const int               g_c_int       = 61;
const int       &       g_c_int_ref   = g_c_int;
      int const &       g_int_c_ref   = g_c_int;
      int       * const g_int_ptr_c   = &g_int;
      int const * const g_int_c_ptr_c = &g_int;
const int       * const g_c_int_ptr_c = &g_int;
      int const *       g_int_c_ptr_c = &g_int;

But the following does not compile:

const int const * const g_c_int_c_ptr_c = &g_int;

The const before or after the int specifies that the int cannot be change. The const after the '*' specifies that the pointer to the int cannot change. When you have three consts, you are specifying that the int is const twice, which is an error.

No comments:

Post a Comment