Paul N <
gw7...@aol.com> wrote:
> I've decided to try to use the C++-style casts. But they look ugly!
They're supposed to look ugly! In most cases you don't need casts
and if they are really required this is typically a point in the
code that needs to stand out. So an ugly and easily to find cast
is a rather good idea in my book. In such places something happens
where the author states "I know better than the compiler" - and I
have seen too many places where this wasn't true...
> The best I have managed so far is to add spaces to give:
> ti.lpszText = const_cast <LPTSTR> (text);
That's a typical cast that would raise an eyebrow: why do you
cast away the const-ness of 'text'? Probably there was some
reason why it was declared as constant? By casting away the
const-ness you just opened a hole for a lot of new bugs. So,
instead of trying to hide the cast you should better add a
long comment why this was unavoidable and leave the cast in
in all it's ugliness for you and others to find easily should
anything go wrong!
> But this is still ugly, and is difficult to read - the round brackets look
> like an optional extra instead of being an essential part of the cast. Does
> anyone have anything better? Or is it the case, as one tutorial seemed to
> hint, that the casts are deliberately ugly to dissuade people from writing
> code that uses them?
Well, don't use casts unless they're really required. And if a
cast is needed anyway but it's benign (but you should be absolutely
sure of that, it shouldn't just be something to make the compiler
stop whining) you can still use C-style casts which stand out less
(but don't explain what kind of cast it is):
ti.lpszText = (LPTSTR) text;
But, repeating myself, the ugliness isn't in the cast itself but
in the design (your's or some library's you may have to use) that
requires the cast.
Regards, Jens
--
\ Jens Thoms Toerring ___
j...@toerring.de
\__________________________
http://toerring.de