pointers for derived and base class

Discussion in 'C++' started by Hisham, Jun 16, 2005.

  1. Hisham

    Hisham Guest

    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 ?
     
    Hisham, Jun 16, 2005
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Jun 16, 2005
    #2
    1. Advertising

  3. Hisham

    Rolf Magnus Guest

    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 = 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.
     
    Rolf Magnus, Jun 16, 2005
    #3
  4. Hisham

    Peter Julian Guest

    "Hisham" <> wrote in message
    news:...
    > 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.
     
    Peter Julian, Jun 16, 2005
    #4
  5. Peter Julian wrote:
    > "Hisham" <> wrote in message
    > news:...
    >
    >>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
     
    Victor Bazarov, Jun 16, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    413
    Alf P. Steinbach
    May 23, 2007
  2. Replies:
    1
    Views:
    397
    myork
    May 23, 2007
  3. Replies:
    1
    Views:
    389
    Victor Bazarov
    May 23, 2007
  4. bgold
    Replies:
    12
    Views:
    1,020
    James Kanze
    Jan 1, 2008
  5. Replies:
    2
    Views:
    715
Loading...

Share This Page