Const-cast Typecast

来源:互联网 发布:交互设计软件 html 编辑:程序博客网 时间:2024/06/09 23:39
Const-cast TypecastConst casts areonly available in C++. Const casts are used to strip the const-ness or volatile-ness from a variable. Const casts should be used sparingly;one example of a valid use of a const-cast is to strip the const-ness of a pointer to pass it into a function when you are certain the function will not modify the variable but the function designer did not specify the input as const. 

const_cast<<type>>(<value>); 

This example casts a const pointer to a non-const pointer to pass into a function: void func(char *); 

const char *x = "abcd";
func(const_cast<char *>(x));
原创粉丝点击