“warning: use of old-style cast” in g++ [duplicate]

匿名 (未验证) 提交于 2019-12-03 03:04:01

问题:

Possible Duplicate:
When should static_cast, dynamic_cast and reinterpret_cast be used?

With this C++ code,

char* a = (char*) b; 

I got warning warning: use of old-style cast.

What would be the new-style cast?

回答1:

reinterpret_cast, static_cast, dynamic_cast and const_cast are the c++ cast alternatives.

  • const_cast to remove const/volatile from a const variable.
  • dynamic_cast to perform runtime validity checks when casting in between polymorphic types
  • static_cast to perform e.g up/down-cast in a inheritance hierarchy, but with no runtime checks, or to explicitly perform conversions that could be implicit (e.g. float to int)
  • reinterpret_cast to convert in between unrelated types.


回答2:

Read this topic to know about C++ style casts which come in various flavors:

When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!