Article View: alt.msdos.programmer
Article #1204Re: typecasting preferences
From: Chris Page
Date: Thu, 18 Apr 1996 00:35
Date: Thu, 18 Apr 1996 00:35
50 lines
2697 bytes
2697 bytes
In article <3173E842.7D17@willows.com>, Tarang Deshpande <tarang@willows.com> wrote: > HASDI RODZMANN HASHIM wrote: > > > > If you have a choice, do you want the parethesis pair over the caster > > (C-style) or over the castee (Pascal-style)? eg... > > > > a = (int)b; /* cast to int */ > > a = int(b); /* allowable in C++ */ > > > > > I would prefer the Pascal style where the parenthesis go around the > thing being casted. This is so that the cast looks like a function > and it then seems consistent when overriding the cast operator. On the one hand, I prefer the "C" style cast because it is explicit that a cast is taking place, whereas the functional style requires you to know whether the function is a cast function or not. Since casting is an important thing to know about in a statically typed language (ignoring RTTI for this discussion), I prefer the emphasis on casting; I think it makes code more readable. On the other hand, the functional style emphasizes that some function may actually be called, and that that function may do significantly more than, for example, sign extension. However, I was just reading "Item 5: Be wary of user-defined conversion functions" in Scott Meyers' More Effective C++, and he basically concludes that you should never define cast operator functions in a class because they can be called unexpectedly (unless, of course, you really really want to :-). Instead of either solution, I prefer the Dylan approach: there is a function "as()" that takes a type parameter and an object parameter and returns an object of the specified type, created using the object parameter. It looks like this: foo := as(<type>, bar); The thing I like about this is that "as()" is defined as part of the language (like, say "new" in C++), so you always know that you are performing a cast, but it also indicates that a function is, or may, be called. Of course, C++ can't currently handle this approach because it doesn't have values that represent types. Perhaps RTTI provides this solution? -- Chris Page | Internet chain letters, advertisements and SPAMs suck... Don't Panic! | page@best.com | Cut it out! :-P
Technical Details
Message-ID:
<page-1704961735120001@page.vip.best.com>Path:
arc1-ca.pugleaf.net!page.vip.best.com!userReferences:
<4l020g$i9j@srvr1.engin.umich.edu> <3173E842.7D17@willows.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!page.vip.best.com!user From: page@best.com (Chris Page) Newsgroups: alt.msdos.programmer,comp.lang.c,comp.lang.pascal.misc Subject: Re: typecasting preferences Date: Wed, 17 Apr 1996 17:35:12 -0700 Organization: Don't Panic! Message-ID: <page-1704961735120001@page.vip.best.com> References: <4l020g$i9j@srvr1.engin.umich.edu> <3173E842.7D17@willows.com> NNTP-Posting-Host: page.vip.best.com X-Newsreader: Yet Another NewsWatcher 2.1.8