返回> 网站首页 

Const-cast Typecast

yoours2008-02-27 10:48:36 阅读 1008

简介一边听听音乐,一边写写文章。

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));
微信小程序扫码登陆

文章评论

1008人参与,0条评论