Re: Opinion on coding style.
- From: "Doug Harrison [MVP]" <dsh@xxxxxxxx>
- Date: Wed, 19 Dec 2007 21:31:05 -0600
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
.
- Follow-Ups:
- Re: Opinion on coding style.
- From: Leo V
- Re: Opinion on coding style.
- References:
- Opinion on coding style.
- From: Leo V
- Opinion on coding style.
- Prev by Date: Opinion on coding style.
- Next by Date: Re: why Visual Studio can not optimize the initialization code?
- Previous by thread: Opinion on coding style.
- Next by thread: Re: Opinion on coding style.
- Index(es):
Relevant Pages
|
Loading