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

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist