Item 9. New Cast Operators

There’s something sneaky and underhanded about old-style casts. Their syntax is such that they can often pass unnoticed in a section of code, but they can do terrible damage, like an unexpected sucker punch from a bully. Let’s clarify what we mean by “old-style” cast. Obviously, the original C syntax consisting of a parenthesized type applied to an expression is an old-style cast:

char *hopeItWorks = (char *)0x00ff0000; // old-style cast

C++ introduced another way of saying the same thing with the function-style cast syntax:

typedef char *PChar;hopeItWorks =                                                PChar( 0x00ff0000 ); // function-style/old-style cast

A function-style cast may look more civilized than its dread ...

Get C++ Common Knowledge: Essential Intermediate Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.