[ fromfile: types.xml id: whynotC-style ]
C-style casts are deprecated and should not be used anymore. Consider the following situation, quite similar to the previous example.
Apple apple; Orange* orangeP; // other processing steps ... orangeP = (Orange*) &apple; orangeP->peel();
The problem is that you cannot tell from looking at this code whether the developer is aware that an Apple
is compatible with an Orange
. From looking at it, it
is unclear whether this is a proper type conversion or a nonportable pointer conversion. Sure, they might both
have peel()
functions, but can you peel an Apple like an Orange?
Errors caused by such a cast can be difficult to understand and correct.
If a system-dependent cast is necessary, it is preferable to use reinterpret_cast
over a C-style cast
so that,
when troubles arise, it will be easier to spot the likely source of those troubles in the source.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |