--- In [email protected], Christopher Coale <chris95...@...> wrote:
>
> John Matthews wrote:
> >
> > --- In [email protected] <mailto:c-prog%40yahoogroups.com>, 
> > Christopher Coale <chris95219@> wrote:
> > >
> > > Jos Timanta Tarigan wrote:
> > > >
> > > > colour *col=new colour[10][10]; <-- i cant compile this line.
> > > >
> > > > cannot convert int(*)[10] to int*
> > > >
> > > You just need to typecast the "new colour[10][10]" to a "colour *".
> > >
> > > colour *col = (colour *)new colour[10][10];
> >
> > Sorry, I don't do C++, but if you needed a typecast in the equivalent
> > C program, I would say you had done something wrong. Please tell me
> > there's a better way :-)
> >
> >  
> Why do you say that? It simply returns a pointer to the first
element in 
> the array, and from there you can access every element inside it.

I've no doubt it works, but in general you should avoid typecasts -
they are unsafe because they bypass all compiler type checking and
allow you to do horrible things, especially where pointers are involved.

There are situations where you need them, but for code which is as
(relatively) simple as this, use of a typecast tells me that something
is wrong. Just because code works, it doesn't mean it's good code, or
even correct code.

If your solution works and is the 'best' one, then I apologise for my
lack of C++ knowledge. However, other posters have suggested there are
better solutions. I would certainly be able to knock something up in C
which works and doesn't require typecasts, which again implies to me
that a C++ solution that does require typecasts isn't ideal.

Cheers
John

Reply via email to