Evo. G Tech Team Forum
Welcome to Evo. G Tech Team Forum. We have moved to a new website : www.evogtechteam.com

Thanks you.

by Evo. G Tech Team Management.

四种强制类型转换之一const_cast

View previous topic View next topic Go down

四种强制类型转换之一const_cast

Post by too wei on June 23rd 2015, 18:29

const_cast
-可以在对象上设置const或消除const
-只有const_cast可以删除const属性
-为了安全性,const_cast只能用于指针,因为在指针之间是可以随意交换

如果你试图使用一个 const_cast 以外的新风格强制转型来代替const_cast,你的代码将无法编译。

int main()
{
const int * cp = new int; //const
*cp = 50; //error

int * p = const_cast(cp); //const_cast把cp转换去non-const
*p = 50; //ok!

const int * temp  = const_cast(p); //const_cast又把p转回去const
*temp = 60; //error

return 0;
}

too wei
Sponsor
Sponsor

Posts : 31
Points : 16551
Reputation : 0
Join date : 2015-04-21
Age : 18
Location : Johor

View user profile

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum