Topic: C++ casts


Author: alexo@log-on.com (Alex Oren)
Date: 1996/10/11
Raw View

Can anyone post (or point me to) a good explanation on the various C++ cast
operators (static_cast<>, dynamic_cast<>, const_cast<>, reinterpret_cast<>,
and the C-style cast) and the difference between them?

Please CC me by email.

Thanxalot!


Have fun,
Alex.



[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: jim brown <jimm@aol.com>
Date: 1996/10/14
Raw View
Alex Oren wrote:
>
> Can anyone post (or point me to) a good explanation on the various C++ cast
> operators (static_cast<>, dynamic_cast<>, const_cast<>, reinterpret_cast<>,
> and the C-style cast) and the difference between them?
> Thanxalot!
>
> Have fun,
> Alex.
>Scott Meyer's new book "More Effective C++" has a nice chapter discussing
all of these.  If I remember correctly, const_cast is used to cast away
const-ness, dynamic_cast is used for safe downcasts (i.e. base pointer =>
derived class pointer), reinterpret cast is for specialized stuff like
casting to/from function pointers with the "wrong" signature (like MS's
old FARPROC typedef), and static_cast.. hmm I think that was for routine
stuff like reducing the length of an integer type (e.g. long to int).

Meyer's book is also a good source for other recent stuff like the
explicit and mutable keywords, typeid/RTTI, auto_ptr, etc.  His publisher
has a Web site with errata and recent changes to auto_ptr.


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: brad@cfar.umd.edu (Brad Stuart)
Date: 1996/10/14
Raw View
Alex Oren (alexo@log-on.com) wrote:

: Can anyone post (or point me to) a good explanation on the various C++ cast
: operators (static_cast<>, dynamic_cast<>, const_cast<>, reinterpret_cast<>,
: and the C-style cast) and the difference between them?

: Please CC me by email.

: Thanxalot!


An excellent article on the cast operators is available at

http://www.cs.rpi.edu/~wiseb/xrds/ovp3-1.html

Thanks to G. Bowden Wise for writing and making this available.

Brad

--
| Brad Stuart      brad@cfar.umd.edu
| Center for Automation Research
| University of Maryland, College Park
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]