Pointers vs. const (C++11)

Source: Internet
Author: User

1.cosnt pointer
    int39;    constint * pt = &age;

You cannot use PT to change the value of age, that is, *PT is const and cannot be modified.

    1;      // 非法操作    cin >> *pt;    // 非法操作

However, the PT declaration does not imply that the variable it points to (age) is const, and simply that it is not possible to use PT to modify the variable it points to (age).

    20;      // 非法操作    20;      // ok
2. Pointer to const object 2.1 A const pointer to a const object
    constfloat9.80;    constfloat * pe = &g_earth;   // ok

Both G_earth and PE are const and cannot be used to modify their corresponding values (9.80).

2.2 Non-const pointer to const object

C + + does not allow a const address to be assigned to a non-const pointer.

    constfloat1.63;    float * pm = &g_moon;   // 非法操作

Of course, you can also use type conversions to break this limit.

3. Pointers pointing to pointers
    constint **pp2;    int *p1;    constint13;    pp2 = &p1;      // 非法操作。先假设合法。    *pp2 = &n;      // ok,两者都是const,但 p1 = &n 非法。    10;       // 此行的语法是合法的,但不能成功修改n。

Therefore, the operation of assigning a non-const address or pointer to a const pointer applies only in the case of an indirect address operation. In other words, const or non-const data can be assigned to a const pointer, as long as the data itself is not a pointer type.

4.const parameters
    const int months[12] = {312831303130313130313031};    ...    int sum(int arr[], int n);  // 应该是 const int arr[]    12);    // 非法

The above function call attempts to assign a const pointer (months) to a non-const pointer (arr), which results in a compilation error.

Small advice:
As long as you can use const, use it. There are two reasons why it is recommended to add a const to a pointer declaration:
1. Can prevent unintentional modification of data;
2. Use the const allow function to handle const and non-const arguments. If you omit const in a function prototype, the function can only accept data that is not const.
As long as you can, it is recommended to declare the formal parameter as a const pointer.

Pointer to const (C++11)

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.