New to C++. Question about constant pointers

前端 未结 10 1907
梦谈多话
梦谈多话 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:08

    As it has already been pointed out the perhaps most common const pointer is

    const char* p;
    

    The variable p can change, but the data p points to is unmodifable.

    However, moving the const keyword to the left of the asterisk does not alter the meaning of the declaration:

    char const* p;
    

    I prefer the later since it becomes much easier to remember where to place the const keywords when declaring const pointers to const pointers:

    char const* const* p;
    

    Again, the variable p can change and the data pointed to is unmodifiable. Furthermore, the data is declared as const pointers meaning that it points to data that cannot be modified.

    The more common notation for this type is

    const char* const* p;
    

    Placing the const keyword immediately to the left of the asterisk it modifies (or ampersand for reference) makes it easy to create complex types involving the const keyword. For example, a pointer to const pointers:

    char const** p;
    

    and a const pointer to pointers:

    char* const* p;
    

    Remember to "read" pointer declarations from the right to the left, and don't declare more than one pointer in each statement to avoid a lot of confusion.

    0 讨论(0)
  • 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.";
    
    0 讨论(0)
  • 2021-02-10 07:12

    G'day,

    To remember this easily you can use the trick that Scott Meyers describes in his excellent book "Effective C++" (sanitised Amazon link)

    You draw a line through the declaration where the asterisk is located.

    • If the keyword const appears to the left of the line, then you can't change the value of the item that you're pointing to.
    • If the keyword const appears to the right of the line, then you can't change the pointer to point to another location.
    • If const appears on both sides, then you can't change the pointer and you can't change the value of what you're pointing to.

    BTW That book is excellent, and while not for a beginner, is definitely a way of taking your C++ knowledge to the next level! Highly recommended.

    HTH

    cheers,

    0 讨论(0)
  • 2021-02-10 07:18

    A simple way to remember how const works with pointers is to remember that it always applies to whatever is to the left of it, unless it's the left-most keyword, in which case it applies to the right.

    Examples:

    Pointer to a constant char: The pointer can be changed to point to something else, but the char it initally points to cannot change value.

    const char * p;
    

    Constant pointer to a char: The pointer cannot be changed to point to anything else, but the char it points to can change value.

    char *const p;
    

    Constant pointer to a constant char: The pointer cannot be changed to point to anything else, and the char it points to cannot change value.

    const char *const p;
    
    0 讨论(0)
  • 2021-02-10 07:20

    A const pointer means that you can change the value of the variable which is being pointed to, but you can't change where the pointer is pointed. I don't use them often myself, but a common application for const pointers is in defining specific memory segments that you need to address. See this question for more information.

    As an aside, you should try to get a compiler on your computer if you can. I've shown myself many times that human brains are poor C++ compilers.

    0 讨论(0)
  • 2021-02-10 07:22

    Your code is not legal. You can't assign to aPointer (except using copy-style initialisation, which in fact is not assignment even though it looks like it) if aPointer is declared const like that.

    But usually when people say "a const pointer", they mean const int * aPointer, not int * const aPointer as you have in your code. The whole internet will explain the difference at the drop of a hat. As far as I know, the term "const pointer" isn't defined in the standard, so we're free to do this even though it's potentially confusing. "Pointer-to-const" would be an unambiguous description, and a pointer-to-const is much more commonly used than a pointer-which-is-itself-const.

    A pointer-which-is-itself-const is used to refer to something, where you won't want the pointer to refer to a different thing at any point in its life. For instance, this is a pointer-which-is-itself-const, because "this object" remains the same object through the execution of a member function. The C++ language has opted not to let you decide part way through that you want to assign some other value to this, to make it refer to some other object. In C++ references often serve that purpose too, since they cannot be "re-seated" to change the referand.

    0 讨论(0)
提交回复
热议问题