Casts, especially array casts

Ali Çehreli acehreli at
Wed Feb 24 18:11:36 PST 2010

Rainer Deyke wrote:

 > C++ casts perform three different logical functions:
 >   - Removing cv-qualifiers. (const_cast)


 >   - Reinterpreting raw bytes. (reinterpret_cast)

Safely though: It won't allow any wild bit representations. For really 
wild stuff, you need to use the C-style cast.

 >   - Converting values. (static_cast/dynamic_cast)

I would like to separate static_cast and dynamic_cast:

- static_cast does two things:

a) can convert values: this does call the user-defined conversion 
operators if necessary

b) static_cast also communicates "trust me, this super class *really* is 
this subclass." In this latter case, no value is converted; rather, the 
type of the pointer value is changed, so that the member access offsets 
will be adjusted accordingly later on, when this pointer is used

- dynamic_cast performs only (b) above, but safely: "if this super class 
is actually this subclass, set the pointer to point to it". Otherwise 
the pointer is set to null. Again, no value conversion here.


More information about the Digitalmars-d mailing list