Re: Opinion on coding style.
- From: ajk <ak@xxxxxxxxxxxx>
- Date: Thu, 20 Dec 2007 15:59:51 +0800
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.
Leo.
IMO one should use the C++ casts instead static_cast<> and
reinterpret_cast<> when appropiate, using the above style isn't good
because with these C++ casts you at least you get some help from the
compiler.
Second of all I think that for effeciency/reliability reasons it would
be better to avoid casts whenever possible, C++ is about type safety
and casting like in your example is basically circumventing one of the
features of C++.
If somebody would code like above I would ask why he didn't do a
proper function from the first place taking the CString arg and int
instead of casting.
With regard to Windows API calls of course it often needed to cast
using C style casting but it is another story when it comes to
functions you write yourself.
just my 2c
/ajk
.
- 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: SetClipboardViewer()
- Next by Date: Re: How Jeff Relf would reverse a string.
- Previous by thread: Re: Opinion on coding style.
- Next by thread: Re: Opinion on coding style.
- Index(es):
Relevant Pages
|