[LCP]Rounding questions

Josh Helmer jhelmer4 at cox.net
Sat Sep 7 19:41:05 UTC 2002


> Finally, when you develop a habit of using unnecessary casts,
> then you are almost certain to include casts that are actually
> wrong -- but your cast prevents the compiler from issuing you
> with a useful diagnostic and then you (and the poor unfortunate
> people who have to clean up after you) have to take time to find
> out something that the compiler could have found for free.
>
> Just don't use casts -- except in the very few cases where they
> are required.  There are extra points for providing a single
> example of such a case.

OK...   If you have an array of structures and you want to sort the array.  
The easiest way to go about it is to write a compare function and call 
qsort().  qsort() will just pass void pointers in to your compare function.  
You will have to use a cast to do any sort of meaningful comparison.

I have also found that if I want to write (somewhat) generic container classes 
the easiest way to go about it is to write a container that just holds void 
pointers.  To get data back out of the container you will have to cast it 
back to it's original form.

Don't get me wrong, I mostly agree with you.  Unnecessary casts should be 
avoided.   At best they are just noise, and at worst they can hide real bugs, 
but I am just trying to earn those points you promised... :-)

Josh




More information about the linuxCprogramming mailing list