Some people may be confused about the sequence of const and * on declaration in C++/C, me too.
Now I think we can distinguish them by this way:
1.only noticing the position of const to *, and we can find that the following statements are same:
const int * foo_int; int const * foo_int_; //same.
2.regarding const as a postpositive attributes,and you will know the content which is const.
int foo = 6; int foo_ = 7; //the foo_int is const, but the pointer to foo_int can be chaged. int const * foo_int = &foo; //illegal, the value cannot be chaged //*foo_int = 7 //okay! foo_int = &foo_; //the pointer to int is const, but foo_int_ can be chaged. int * const foo_int_ = &foo_int; //illegal, the pointer cannot be changed. //foo_int = &foo_int_; //even if the new pointer is same, it‘s illegal //foo_int = &foo_int; //okay *foo_int = 8;
时间: 2024-10-24 04:18:18