Re: Opinion on coding style.
- From: "Leo V" <LeoV@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 20 Dec 2007 15:42:16 -0800
"Doug Harrison [MVP]" <dsh@xxxxxxxx> wrote in message
news:05ojm3d6ka8d55qaum9lcd1tr3v8efbget@xxxxxxxxxx
On Wed, 19 Dec 2007 17:01:29 -0800, "Leo V"In the case of casting a DWORD to an int, it is done to avoid compiler
<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?
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).
Yes, there are times when more appropriate style casting happens. A lot of
2. Do they using new-style casts (i.e. static_cast and friends) when
casting is actually appropriate?
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
.
- Follow-Ups:
- Re: Opinion on coding style.
- From: Doug Harrison [MVP]
- Re: Opinion on coding style.
- References:
- Opinion on coding style.
- From: Leo V
- Re: Opinion on coding style.
- From: Doug Harrison [MVP]
- Opinion on coding style.
- Prev by Date: Re: Every DISPATCH_PROPERTYGET needs a matching " Whatever->Release() ".
- Next by Date: Re: Opinion on coding style.
- Previous by thread: Re: Opinion on coding style.
- Next by thread: Re: Opinion on coding style.
- Index(es):
Relevant Pages
|
Loading