来自《c++primer第5版》,我的疑问是粗体:
const_cast
const_cast只能改变运算对象的底层const
const char *pc;
char *p=const_cast<char*>(pc);//正确:但通过p写值是未定义的后果
对于将常量对象转换成非常量对象的行为,我们一般称其为“去掉const性质”。一旦我们去掉了某个对象的const性质,编译器就不再阻止我们对该对象进行写操作了。如果对象本身不是一个常量,使用强制类型转换获得写权限是合法的行为。
上面这句话是什么意思?是说
char *pc;
const char *p=const_cast<const char*>(pc);
用来对p写值是合法的行为吗?我觉得反过来才对吧?(我已经懂了,作者意思应该是说非常量constcast 成常量后再const cast成非常量的这种情况是可以获得写权限的)
然而如果对象是一个常量,在使用const_cast执行写操作就会产生未定义的后果。
我想const_cast
的目的就是用来对const对象写值,如果“通过p写值是未定义的后果”那么使用const_cast有什么用呢?
What does the above sentence mean?
Answer: Even if const_cast can remove the constness of a pointer or reference (go to const) and obtain a non-const pointer or reference, using the converted pointer or reference to modify an object originally declared as const will cause unforeseen consequences. Define behavior. Example
If j is declared as
int j = 3;
it is not a constant, the above code is legal and expected to work.Answer: The following uses:
The most common thing is that when some people declare a function type, they do not declare it as const even if the value of the pointer is not modified in the function. This is typical laziness, such as
void log(char* msg);
Obviously it is just used to print out msg, but it does not add const. In this case, we have to convert the const pointer on our hand to non-const and then pass it to it, because the c++ compiler does not allow you to directly throw the const pointer to char*, and an error will be reported, while c compilation, such as gcc, only generates Just a warning.
I saw this usage in effective c++, sample code:
In short, in a const member function, you need to modify the member variables that are not declared as mutable.
You can remove volatile-modified variables. The specific function remains to be understood.
First of all
This sentence means that to convert an expression from const to non-const, or vice versa, only const_cast can be used, and other forms such as
This won’t work.
Then
This sentence means that when using const_cast, the type to be converted and the type being converted must be consistent. The only difference is whether there is a const modifier, such as
This is not possible because the type of cp is char*, which is completely different from string. If cp is const string cp, it will work.
But
staticcast<string>(cp) is feasible because it does not change const, it just changes the type, which does not fall into the two situations mentioned above with constcast.
In fact, the content of the book is condensed, and the codes are put together to save space, which leads to misunderstanding.
First of all, whether the const modifier is added or not will constitute operator overloading. The counterpart to const is volatile, collectively called the cv modifier.
I think the purpose of constcast is to write values to const objects. If "writing values through p is an undefined consequence", then what is the use of using constcast?
const_cast is just a compile-time convention, and "writing values through p is an undefined consequence" is a runtime problem. These two constraints are not the same thing
It was also said above" Only const_cast can change the constant attributes of an expression. Using other forms of named casts to change the constants of an expression will cause a compiler error." However, the following example is
Your example is correct because You used static_cast to convert