C + + Should the const be placed before or after the type of _c language

Source: Internet
Author: User

I put it in the front, but it's just a matter of personal preference. "Const T" and "T const" are always allowed, and are equivalent. For example:

  const int a = 1; OK
  int const B = 2;//also OK

I suspect that the first version may be confusing to a handful of programmers (who are more likely to stick to the rules of grammar).

Why? When I invent "const" (the original name is called "ReadOnly" and there is a corresponding "WriteOnly"), I allow it to appear before or after the type, because it does not bring any uncertainty. Pre-standard C and C + + Specify a few (if any) specific order specifications.

I don't remember having any in-depth thoughts or discussions on the sequencing issue at that time. At that time, some of the early adopters--especially me--just liked it:

  const int c = 10;

Looks better than this:

  int const C = 10;

Perhaps I have also been affected by this: in my earliest examples of using "ReadOnly"

  readonly int c = 10;

is more readable than this:

  int readonly C = 10;

The first "const" (c or C + +) code I've created seems to have replaced the "readonly" around the world.

I remember that the choice of grammar was discussed in several people, such as Dennis ritchie--, but I don't remember what language I was leaning towards.

Note In the fixed pointer (const pointer), "const" always appears after "*". For example:

  int *const p1 = q; Fixed pointer
  int const* p2 = q pointing to int variable://pointer to int constant
  int* p3 = q;//pointer to int constant

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.