"Nick van Eijndhoven" <Nick_at_phys.uu.nl> writes:
> Since I have MSVC++ 7.1 on my winXP box I think I am safe,
> but I don't want to be forced to use this ugly looking
> (and hard to read) new casting statements in my private classes.
> I (and most of my colleagues and students) am a physicist who
> wants to keep things as simple and transparent as possible ;).
I can't resist not tossing in my 2cent rant here.
I find C-style casts *completely* opaque as they allow overlaying arbitrary structure onto arbitrary memory with no checks whatsoever.
OTOH, C++ casts provide a declaration of programmer intent as well as a large amount of compile time and/or run time correctness checks.
With C-style:
SomeRandomClass *ptr = (SomeRandomClass*)someRandomObject;
in order to understand this, I have to:
However if I see:
SomeRandomClass *ptr = dynamic_cast<SomeRandomClass*>(someRandomObject);
I know the intent of the programmer and can rely on the compiler to do the necessary checking, some at compile time, some at run time. I can be assured that if this is a wrong cast to make it will fail quite visibly (unless, of course, the code is being interpreted by CINT).
If I see:
SomeRandomClass *ptr = static_cast<SomeRandomClass*>(someRandomObject);
or even
SomeRandomClass *ptr = reinterpret_cast<SomeRandomClass*>(someRandomObject);
At least I know the programmer explicilty realizes s/he is doing something a bit wonky. And, with static_casts some checks are done by the compiler.
That last example is essentially a C-style cast, but at least they are trivial to find, so if you suspect a bad cast is leading to some bug, at least it's easy to find all the instances. Short of a full C++ parser, it virtually impossible to find all instances of C-style casting.
> This may sound oldfashioned, but too many times I have experienced
> that this simplicity pays off at the end.
Life is not simple.
-Brett. Received on Fri May 13 2005 - 17:10:11 MEST
This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:08 MET