POV-Ray : Newsgroups : povray.programming : POV-Ray 3.5 for AmigaOS : Re: Bah. Server Time
3 Jul 2024 06:23:47 EDT (-0400)
  Re: Bah.  
From: Warp
Date: 11 Jul 2004 05:23:51
Message: <40f10727@news.povray.org>
Wolfgang Wieser <wwi### [at] nospamgmxde> wrote:
> The "correct C++ way" would be to use static_cast<int>(). 

  static_cast is actually the only "clean" casting type. All the three
other casts are more or less ugly casts which usually shouldn't be
used in well-designed code. Usually if you need to use the other casts,
it's a sign that there's a flaw in your design (even though there are
a few exceptions, but as a rule of thumb).

  static_cast performs a cast between compatible types. Although
casting between compatible types does not usually need an explicit
cast (implicit casting is usually ok), it's usually recommended to
do the cast explicitly when the cast is done from a "larger" type
to a "smaller" one (eg. from int to char, or from double to int, etc).
It can be thought as a kind of "documentation" which says "yes, I really
want to cast even at the risk of losing significant digits".
  static_cast should fail (ie. give a compiler error) if the cast is
performed between inompatible types (for example trying to convert
an int to a pointer).

  There's actually one situation more where static_cast can be used:
When downcasting a base-class type pointer to a derived-class type.
The compiler will not complain about this, but it's a bit dangerous
because you must be sure that the base-class type pointer really points
to the derived-class type you are casting to or else malfunction will
follow. There are a few cases where this is ok, but usually if you need
to downcast at all, you should use dynamic_cast instead.

  dynamic_cast converts between class pointers, but it checks at runtime
that the cast is valid. If the cast is invalid, it returns a 0 pointer.
This is usually done for downcasting a pointer and checking that it
succeeded.
  Usually, though, if you need to downcast, it's a sign of bad OO design.

  const_cast casts a const pointer to its non-const version. If you need
to use it, there's a flaw in your design. (The only place you should be
forced to use it is when having to use someone else's library which is
badly designed.)

  reinterpret_cast casts between incompatible types (eg. from an int to
a pointer or between pointers to completely unrelated types). Usually
only needed if you are making low-level hacker code, usually not needed
for high-level OO code.

-- 
#macro M(A,N,D,L)plane{-z,-9pigment{mandel L*9translate N color_map{[0rgb x]
[1rgb 9]}scale<D,D*3D>*1e3}rotate y*A*8}#end M(-3<1.206434.28623>70,7)M(
-1<.7438.1795>1,20)M(1<.77595.13699>30,20)M(3<.75923.07145>80,99)// - Warp -


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.