i would say const in c++ is powerful. Inside a function I can make a const to the pointer or to the value. For example,
int d = 5;
int x = 6;
int * const a = &d;
a = &x; // it will error because the pointer is constant.
But if you put const in the left side of *, (const int * a) it wont error because the constant is only the value. Here, you modify the pointer.
To make it easy to read, if the const is in the right of *, so it is pointer constant. if it is in the left side, it is value constant.
const can also be used in the function. They can be put in the left or right side of the function and they have different meaning.
void foo() const.
If the const word is in the right side of the function, it means you cannot change any member of class.
If it is in the left side of the function, I still don't know what it is used for.
No comments:
Post a Comment