Skip to content

Latest commit

 

History

History
33 lines (24 loc) · 1.95 KB

casting.md

File metadata and controls

33 lines (24 loc) · 1.95 KB

Downcasting and Upcasting

The oldest reference I've found yet is from September 1990, in a Usenet post. This uses the term “castdown”.

The library referenced there is the NIHCL (available from the Software Preservation Group), which contains this code (MI is "multiple inheritance"):

#ifdef MI

#define DECLARE_CASTDOWN(classname) \
    static classname& castdown(Object& p) \
        { return *(classname*)(&p ? p._safe_castdown(*desc()) : 0); } \
    static const classname& castdown(const Object& p) \
        { return *(const classname*)(&p ? p._safe_castdown(*desc()) : 0); } \
    static classname* castdown(Object* p) \
        { return (classname*)(p ? p->_safe_castdown(*desc()) : 0); } \
    static const classname* castdown(const Object* p) \
        { return (const classname*)(p ? p->_safe_castdown(*desc()) : 0); } \

#else

#define DECLARE_CASTDOWN(classname) \
    static classname& castdown(Object& p)           { return (classname&)p; } \
    static const classname& castdown(const Object& p)   { return (const classname&)p; } \
    static classname* castdown(Object* p)           { return (classname*)p; } \
    static const classname* castdown(const Object* p)   { return (const classname*)p; } \

#endif

The book that this code was included with (Data Abstraction and Object-Oriented Programming in C++, 1990) also uses the term “castdown”.

The term "castdown" also seems to predate “downcast”, at least on Usenet.