On Fri, Jan 9, 2009 at 1:55 PM, Tyler Littlefield <[email protected]> wrote: > I still haven't figured out why people avoid type casts like the plague. > You'll survive through their usage. If you type cast things just to "make > them work," then it is bad practice, but for something like that it's > harmless. People suggested wrapping the array up in a struct/class, which > requires an object per-array that you want to pass around, (tacky?). People > have also suggested containers. As he hasn't gotten the hang of pointers > quite yet (or so it seems from post), I'd assume that he'd like to just > stick to a 2-d array, and not some "better method."
Typecasting can be a source of very hard to detect errors. It is basically a way of telling the compiler 'this is a pointer to apple' when you have say a pointer to a peach, so in essence you're lying to the compiler to silence some errors. One of C++'s design goals were to avoid the 'pointer hell' that could ensue in a C program when you're abusing pointers and typecasts. Ideally most C++ programs should not use pointers at all, instead use containers to store multiple instances. Carrying over C-style paradigms to C++ is bad practice. Beginners of C++ should not be concerned with pointers at all. One of the best starting books for C++ is Koenig and Moo's Accelerated C++, which avoids pointers until something like chapter 10. In case you absolutely would like to do casting in C++, then use C++ style casts (static_cast, dynamic_cast, reinterpret_cast), instead of plain C-style casting, as they can catch errors the latter can't. -- Tamas Marki
