Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > pointers for derived and base class

Reply
Thread Tools

pointers for derived and base class

 
 
Hisham
Guest
Posts: n/a
 
      06-16-2005
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;
for(i=0;i<10;i++)
*(dr+i)=new drv //it will compile but in excuting the program it say
unhandled exception and terminate the program
can someone tell me why ?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-16-2005
Hisham wrote:
> hey all i have problem here
> suppose i made class base and i derived drv that has function set data
> in the base class


You should consider writing C++ instead of English in a situation
like this. (a) It is less open to interpretation and (b) many of us
here have English as a second language and C++ as our first which
makes C++ much easier to understand than English. Let me illustrate.
What I understood from your second sentence (and do put . at the end
of the sentence please) is

class base { public: void set_data(); };
class drv : public base {};

> 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;
> for(i=0;i<10;i++)
> *(dr+i)=new drv //it will compile but in excuting the program it say
> unhandled exception and terminate the program
> can someone tell me why ?


The code as posted can't be compiled. Please follow the instructions
in the FAQ 5.8 and make another attempt at stating your problem. As
in many other's beginners' posts, you omit some essential parts that
actually [most likely] cause the run-time error you're asking about.

V


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      06-16-2005
Hisham wrote:

> 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;


More readable would be:

ba[i] = new base;

> for(i=0;i<10;i++)
> *(dr+i)=new drv //it will compile but in excuting the program it say
> unhandled exception and terminate the program
> can someone tell me why ?


Please try to make a minimal but complete (i.e. compilable as is) program
that still shows the behavior and then post it here.

 
Reply With Quote
 
Peter Julian
Guest
Posts: n/a
 
      06-16-2005

"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.

Why not just write:
base* ba[20];
and provide a virtual destructor for your base class? Better yet, use a
std::vector.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-16-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting from base to derived class in base constructor pastbin@gmail.com C++ 2 02-07-2008 02:41 PM
Base Class pointers to Derived Classes calling functions. bgold C++ 12 01-01-2008 11:15 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) developereo@hotmail.com C++ 4 05-23-2007 09:32 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57