Peter Julian wrote:
> "Hisham" <> wrote in message
> news: oups.com...
>
>>hey all i have problem here
>>suppose i made class base and i derived drv that has function set data
>>in the base class
>>when i want to have array of pointers for both of them
>>base* ba[10];
>>drv* dr[10];
>>then i want to do this
>>for(int i=0;i<10;i++)
>>*(ba+i)=new base;
>
>
> ba[i] = new base;
>
>
>>for(i=0;i<10;i++)
>>*(dr+i)=new drv //it will compile but in excuting the program it say
>
>
> dr[i] = new drv;
>
>
>>unhandled exception and terminate the program
>>can someone tell me why ?
>>
>
>
> Be carefull how you delete the array elements, you can't do:
> delete [] ba;
> delete [] dr;
> Since the array is an array of pointers, not objects.
The arrays are automatic, they don't need to be deleted. Each object
does, however, need to be deleted, in a loop.
> Why not just write:
> base* ba[20];
> and provide a virtual destructor for your base class? Better yet, use a
> std::vector.
Without knowing how the arrays are used it's impossible to suggest
anything, really.
V
|