Re: Opinion on coding style.




"Doug Harrison [MVP]" <dsh@xxxxxxxx> wrote in message
news:05ojm3d6ka8d55qaum9lcd1tr3v8efbget@xxxxxxxxxx
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?
In the case of casting a DWORD to an int, it is done to avoid compiler
warning.
In the case of the CString object, it's a bit more complicated why LPCTSTR
is specified
rather than letting the compiler interpret and automatically call the
LPCTSTR operator. But,
I'll just say that it is because we actually use about 3 different string
classes. And, only CString
works well on a Format(...) type statement. The other string classes cause
a crash if you don't
force an LPCTSTR operator call. So.... for consistency we always cast to
LPCTSTR on
Format(...) type statements, even if the string is a CString (you never know
when another dev
will come in and replace that CString with std::string or something like
that).


2. Do they using new-style casts (i.e. static_cast and friends) when
casting is actually appropriate?
Yes, there are times when more appropriate style casting happens. A lot of
this is older code.
I'm was just wondering if I was correct in discouraging casting to base
types using this style:
basetype(othertype) when basetype is not a class.

--
Doug Harrison
Visual C++ MVP


.



Relevant Pages

  • 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: 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: 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