C++ const Keyword Usage

In C++, Programming by timfanelliLeave a Comment

Just as a follow up to my last post, here’s a quick overview of the const keyword

1. const int x = 5;
2. const int * px;
3. int const * px;
4. const int * const px;
  1. A constant integer with a value of 5. The value of x will be 5 for its entire life.
  2. A pointer to a constant integer. The value of the integer pointed to by px can not be changed; however you may point px to a different integer.
  3. This is the same as #2. The const keyword may appear before or after the type it is qualified on. I prefer the style in #2, but both are syntactically correct.
  4. A constant pointer to a constant integer. You can not change the value of the integer being pointed to, nor can you point px to a different integer.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.