www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.ide
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript
electronics



digitalmars.D - Re: rtti cast

↑ ↓ ← terranium <spam here.lot> writes:
Jarrett Billingsley Wrote:

 I've only ever used 
 downcasts in D as a replacement for Java's "instanceof", where it's just a 
 simple typecheck. 

cast is not a typecheck, it's a type conversion.
May 08 2008
↑ ↓ "Janice Caron" <caron800 googlemail.com> writes:
2008/5/8 terranium <spam here.lot>:
  cast is not a typecheck, it's a type conversion.

In C++, there is a difference between a regular cast and a dynamic_cast. The expression dynamic_cast<T>(x) performs a run-time type check and type conversion. See http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/topic/com.ibm.vacpp6m.doc/language/ref/clrc05keyword_dynamic_cast.htm dynamic_cast<T>() returns a null pointer of type T if the cast fails. Users of dynamic_cast<T>() are therefore required always to check the return value for null. This is not an indication of a bug - it is designed behavior. Thus, in C++ (1) traditional cast is not a typecheck, it's a type conversion (2) dynamic cast is a a runtime type check Unfortunately, D's cast(T)x behaves exactly like C++'s dynamic_cast<T>(x) when it comes to casting up and down a class heirarchy. D only has one kind of cast, and it does both jobs. So Jarrett is using D's casts correctly, /but/ D's casts are themselves flawed. The "one cast that does everything" ideology is just a tool for making bugs.
May 08 2008
↑ ↓ → terranium <spam here.lot> writes:
Janice Caron Wrote:

 (1) traditional cast is not a typecheck, it's a type conversion
 (2) dynamic cast is a a runtime type check
 

May 08 2008