New to C++. Question about constant pointers

前端 未结 10 1914
梦谈多话
梦谈多话 2021-02-10 06:55

I am trying to learn C++ via some web tutorials. I don\'t have a compiler available to me, otherwise I would try this out. I\'m not sure what is meant by a const pointer. Doe

10条回答
  •  粉色の甜心
    2021-02-10 07:11

    In your code, the pointer cannot move, but the data pointed to can change.

    It's legal up to the first delete. A subsequent new would not work because it's an assignment to a constant.

    It's relatively unusual to see this, more common is to see where the data pointed to is unchangeable but the pointer can move.

    int bar;
    int baz;
    const int * foo = &bar;
    *foo = 4; // illegal
    foo = &baz; // legal
    

    having both pointer and value being const is common with strings

    const wchar_t * const myString = L"String that will never change.";
    

提交回复
热议问题