const pointer assign to a pointer

前端 未结 1 603
故里飘歌
故里飘歌 2020-12-13 15:49

Why can I not do this:

char* p = new char[10];

void SetString(char * const str)
{
    p = str;
}


SetString(\"Hello\");

I have a const po

相关标签:
1条回答
  • 2020-12-13 16:10

    There is difference between constant pointer and pointer to constant. Constant pointer is a pointer (a number - memory address) that cannot be changed - it always point to the same object given via initialization:

    int * const const_pointer = &some_int_var; // will be always pointing to this var
    const_pointer = &some_other_var; // illegal - cannot change the pointer
    *const_pointer = 2; // legal, the pointer is a pointer to non-const
    

    Pointer to constant is a pointer whose pointed value cannot be changed:

    const int * pointer_to_const = &some_int_var; // doesn't have to be always pointing to this var
    pointer = &some_other_var; // legal, it's not a constant pointer and we can change it
    *pointer = 2; // illegal, pointed value cannot be changed
    

    You can always assign constant to variable i.e. const pointer to non-const pointer (a). You can cast pointer to non-const to a pointer to const (b). But you cannot cast pointer to const to a pointer to non-const (c):

    int * pointer;
    int * const const_pointer = &var;
    const int * pointer_to_const;
    
    /* a */
    pointer = const_pointer; // OK, no cast (same type)
    
    /* b */
    pointer_to_const = pointer; // OK, casting 'int*' to 'const int*'
    
    /* c */
    pointer = pointer_to_const; // Illegal, casting 'const int*' to 'int*'
    

    [EDIT] Below, this is not standard c++. However, this is common.[/EDIT]
    String literal

    "Hello"
    

    is converted to constant pointer to const (const char * const):

    char *pointer = "Hello"; // Illegal, cannot cast 'const char*' to 'char*'
    char * const const_pointer = "Hello"; // Illegal, cannot cast 'const char*' to 'char*'
    const char * pointer_to_const = "Hello"; // OK, we can assign a constant to a variable of the same type (and the type is 'const char*')
    "Hello" = pointer_to_const; // Illegal cannot re-assign a constant
    

    In above examples the second is your case. You tried to initialize pointer-to-non-const with a pointer-to-const when passing string literal as argument of your function. No matter if these pointers are constants or not, it's matter what do they point to.

    Summary:
    1) If you cast a pointer of some type to a pointer of another type, you cannot cast pointer-to-const to pointer-to-non-const.
    2) If you have constant pointer, the same rules applies as to other constants - you can assign a constant to a variable but you cannot assign a variable to a constant (except initializing it).

    // EDIT
    As GMan pointed out, the C++98 standard (§4.2/2) allows to implicitly cast string literals (which are constant char arrays) to a non-const char pointer. This is because of backward compatibility (in C language there are no constants).

    Of course such a conversion can lead to mistakes and compilers will violate the rule and show an error. However, GCC in compatibility mode shows only a warning.

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