开发者

Making sense of where "const" goes in a declaration

开发者 https://www.devze.com 2023-03-18 10:40 出处:网络
I am having trouble finding an intuitive pattern for the way const is used in declarations in the C and C++ languages.Here are some examples:

I am having trouble finding an intuitive pattern for the way const is used in declarations in the C and C++ languages. Here are some examples:

const int a;    //Const integer
int const a;    //Const integer
const int * a;  //Pointer to constant integer
int * const a;  //Const pointer to an integer
int const * a const;    //Const pointer to a const integer

In lines 1 and 2, it seems const can com开发者_如何学JAVAe before or after int, which is what it modifies.

  1. So how, in line 4, does the compiler decide that const is modifying * (pointer) rather than int?
  2. What is the rule that the compiler follows for deciding which thing the const applies to?
  3. Does it follow the same rule for *?


Assuming you always place const to the right of the type, you can read a variable declaration as a sentence from right to left:

int const x;        // x is a constant int
int *const x;       // x is a constant pointer to an int
int const *x;       // x is a pointer to a constant int
int const *const x; // x is a constant pointer to a constant int

This still works if you put const to the left of a type, but requires a bit more mental effort. Note that this works just as well with pointers-to-pointers (and higher order constructs):

int *const *const x; // x is a constant pointer to a constant pointer to an int


The compiler generally reads the type from right-to-left, so:

T const& const

Would be read as:

 const (a constant)
 & (to a reference)
 const (to a constant)
 T (of type T)

So, basically the keyword "const" modifies everything that precedes it. However, there is an exception in the case where "const" comes first, in which case it modifies the item directly to the right of it:

const T& const

The above is read as:

const (a constant)
& (to a reference)
const T (to a constant of type T)

And the above is equivalent to T const& const.

While this is how the compiler does it, I really just recommend memorizing the cases "T", "const T", "const T&", "const T*", "const T& const", "const T* const", "T& const", and "T* const". You will rarely encounter any other variation of "const", and when you do, it's probably a good idea to use a typedef.


For pointers, here's one I picked up from one of Scott Meyers's books (I think). Draw a vertical line through the *, and then whatever's on the same side of the line as the const keyword is the thing that's const.

To clarify:

int * const a means that a is const, not the int. And "a" is a pointer to the (non-const) int.


Well, to start off, this question is more of a personal preference. For the leisure programmer, it's more of a personal style. However, for those that deal with corporates, there can be some kind of coding convention that they layout for programmers to follow such that everyone comes out with consistent coding styles.

(1) For const int * const a; it means that you can't change what your pointer points to but you can modify that memory location.

(2) The 'const' is decided by you, as the programmer, whether you want the address the pointer points to, to be constant or if you want the pointer to NOT modify the pointer it points to.

(3) Yes the rules are the same for * as in the case of const int * const a;

As an additional note, your last line is not valid C89.

Hope it helps. Cheers!


The key to understanding these is to realise that the * binds to the a, not to the type. So you should read these as:

const int a;    // a has type const int
int const a;    // a has type int const
const int * a;  // *a has type const int
int * const a;  // *(const a) has type int, which means *a has type int and a is const
int const * a const;    // *(const a) has type int const, which means *a has type const int and a is const.

(Note though that C++ references do not follow this rule).

0

精彩评论

暂无评论...
验证码 换一张
取 消