pointers for derived and base class

H

Hisham

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 ?
 
V

Victor Bazarov

Hisham said:
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
 
R

Rolf Magnus

Hisham said:
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 = 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.
 
P

Peter Julian

Hisham said:
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 = new base;
for(i=0;i<10;i++)
*(dr+i)=new drv //it will compile but in excuting the program it say

dr = 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.
 
V

Victor Bazarov

Peter said:
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 = new base;

for(i=0;i<10;i++)
*(dr+i)=new drv //it will compile but in excuting the program it say


dr = 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top