Re: Opinion on coding style.

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"ajk" <ak@xxxxxxxxxxxx> wrote in message
news:bq7km357tugndo0i297v4g828nhp1u87tg@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.

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


I don't disagree with any of this. It's old code. Old coders. I'm
just asking if you consider only what I have shown. Would one
be a poorer (less clear?) style than the other.



.



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