Re: Opinion on coding style.



On Wed, 19 Dec 2007 17:01:29 -0800, "Leo V"
<LeoV@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

Where I work, I'm finding more code like the following:

assume:
void foo2(LPCTSTR pszVal, int iVal);

void foo()
{
CString str(_T("Some Text"));
DWORD dwVal = 40;
foo2(LPCTSTR(str), int(dwVal)); // I prefer the cast syntax:
foo2((LPCTSTR)str, (int)dwVal);
}

I don't like the way they are casting to LPCTSTR and int. Although, it does
appear to work. But the syntax is implying that you are constructing a
temporary instance of an object. Since LPCTSTR and int are not classes, I
think that coding style is misleading.

In my opinion, if you are doing a cast, then code it as a cast, not as a
copy constructor that doesn't exist.

Before I start coming down on some of the developers where I work, I thought
that I'd first get some of your thoughts.

The two forms are equivalent, so it's a matter of style; of course, the
old-style cast is more flexible, as it works for types consisting of more
than one word. There are two things I'm curious about:

1. Why in the world are they performing unnecessary casts?

2. Do they using new-style casts (i.e. static_cast and friends) when
casting is actually appropriate?

--
Doug Harrison
Visual C++ MVP
.



Relevant Pages

  • Re: Opinion on coding style.
    ... I don't like the way they are casting to LPCTSTR and int. ... In my opinion, if you are doing a cast, then code it as a cast, not as a ...
    (microsoft.public.vc.language)
  • Re: what will happen after i use free()???
    ... Inserting a cast before malloc is not needed, ... 56 bits while int is only 48 bits. ... So casting that value requires some ... Use cast only if you are absolutely sure that yor really needs the ...
    (comp.lang.c)
  • Re: Simple Casting Question
    ... only in the form (int to float),,, ... or just casting identical types, so I hope that I can avoid some ... None of the conversions you describe ... In most cases where a cast is used, ...
    (comp.lang.c)
  • Re: Opinion on coding style.
    ... I don't like the way they are casting to LPCTSTR and int. ... In my opinion, if you are doing a cast, then code it as a cast, not as a ...
    (microsoft.public.vc.language)
  • Re: Opinion on coding style.
    ... I don't like the way they are casting to LPCTSTR and int. ... In my opinion, if you are doing a cast, then code it as a cast, not as a ...
    (microsoft.public.vc.language)

Loading