devxlogo

Prefer const_cast to C-style cast to Remove Object “constness”

Prefer const_cast to C-style cast to Remove Object “constness”

When you have to “unconst” an object, as in this case:

 void print( unsigned char* c) { cout

Instead of removing the "constness" of c this way:

 print( (unsigned char *) c); //C-style cast; not recommended

Use the built-in const_cast operator instead:

 print( const_cast (c) ); //C++ style

What are the advantages? First, you make your code more readable, since your intention is now clearer: you

devx-admin

Share the Post: