Skip to main content
added 84 characters in body
Source Link
codeling
  • 11.5k
  • 6
  • 48
  • 76

constconst always applies to the type to its immediate left; if there is none, it applies to the next type on its right.

So the following three declarations

const static int SECS = ...60 * MINUTE; // or static const int SECS = ...60 * MINUTE; // or static int const SECS = ...60 * MINUTE; 

are all equal. static applies to the whole declaration; and const applies to the int type.

The position of const would only make a difference if you had a "more complicated" type, like e.g. a reference or a pointer:

int a; const int * b = a; // 1. int * const c = a; // 2. 

In this case there is a difference between the place of const - for 1. it applies to the int (i.e. it is a pointer to a const int, i.e. you can't change the value), and for 2., it applies to the pointer (i.e. you can't modify where c points to).

const always applies to the type to its immediate left; if there is none, it applies to the next type on its right.

So

const static int SECS = ... static const int SECS = ... static int const SECS = ... 

are all equal. static applies to the whole declaration; and const applies to the int type.

The position of const would only make a difference if you had a "more complicated" type, like e.g. a reference or a pointer:

int a; const int * b = a; // 1. int * const c = a; // 2. 

In this case there is a difference between the place of const - for 1. it applies to the int (i.e. it is a pointer to a const int, i.e. you can't change the value), and for 2., it applies to the pointer (i.e. you can't modify where c points to).

const always applies to the type to its immediate left; if there is none, it applies to the next type on its right.

So the following three declarations

const static int SECS = 60 * MINUTE; // or static const int SECS = 60 * MINUTE; // or static int const SECS = 60 * MINUTE; 

are all equal. static applies to the whole declaration; and const applies to the int type.

The position of const would only make a difference if you had a "more complicated" type, like e.g. a reference or a pointer:

int a; const int * b = a; // 1. int * const c = a; // 2. 

In this case there is a difference between the place of const - for 1. it applies to the int (i.e. it is a pointer to a const int, i.e. you can't change the value), and for 2., it applies to the pointer (i.e. you can't modify where c points to).

Source Link
codeling
  • 11.5k
  • 6
  • 48
  • 76

const always applies to the type to its immediate left; if there is none, it applies to the next type on its right.

So

const static int SECS = ... static const int SECS = ... static int const SECS = ... 

are all equal. static applies to the whole declaration; and const applies to the int type.

The position of const would only make a difference if you had a "more complicated" type, like e.g. a reference or a pointer:

int a; const int * b = a; // 1. int * const c = a; // 2. 

In this case there is a difference between the place of const - for 1. it applies to the int (i.e. it is a pointer to a const int, i.e. you can't change the value), and for 2., it applies to the pointer (i.e. you can't modify where c points to).