Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

casting

g++

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?

like image 379
prosseek Avatar asked Mar 09 '11 17:03

prosseek


2 Answers

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.

Brief syntax example:

char* a = (char*) b; 
//would be 
char* a = static_cast<char*>(b);
//to remove the warning
like image 181
Erik Avatar answered Nov 15 '22 13:11

Erik


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?

like image 25
Nawaz Avatar answered Nov 15 '22 12:11

Nawaz