C ++のconst int型とのconst int型の違い

  1. オブジェクトが反対されていない場合、違いはありません
int const x = 3;
const int x = 3;
  1. オブジェクトがポインタである場合、それらは異なっている
    int* const p = &array:pは他のポインタのアドレスをポイントすることができません
    const int* p = &array:ポインタpは読み取り専用で&array、変更することはできません

たとえば、

#include <iostream>
 
using namespace std;

int main()
{
    int arr[3]={1,2,3};
    int varr[3]={100,200,300};
    const int* p1 = arr;
    int* const p2 = arr;

    cout << *p1 << endl;
    cout << *p2 << endl;

    // *p1 = 22; // error
    *p2 = 22;
    cout << *p2 << endl;
    cout << arr[0] << endl;

    p1 = varr;
    cout << *p1 << endl;

    p2 = varr;//error
    return 0;
}

おすすめ

転載: www.cnblogs.com/yaos/p/12099521.html