Delete instance of a class array

This is a discussion on Delete instance of a class array within the C++ Programming forums, part of the General Programming Boards category; I have a created the following class instances of Doctor called "room" but I am not sure how to delete ...

  1. #1
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36

    Delete instance of a class array

    I have a created the following class instances of Doctor called "room" but I am not sure how to delete them, one at a time.

    Code:
    const short maxRooms = 6;
    Doctor *room = new Doctor [maxRooms];
    
    
    delete room[0];    // Does not work?
    delete room[1];
    Suggestions?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you allocate all doctors as a group, then you need to delete them in one group too.

    If you want to delete them independently, then you need to do a loop that iterates for maxrooms steps over new Doctor.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36
    I tried:

    Code:
    for (int x = 0; x < maxRooms; x++)
       Delete room[x];
    It will not even compile.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    More like:
    Code:
    delete[] room;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    not even if you spell "delete" with a lower-case?

    If so, what's the error message(s)?

    And you of course need the corresponding loop around "new", yes?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36
    Well, I cannot test it now but I will check on the first suggestion tonight and report back if it does not work, with the error message

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, Laserlight is suggesting the correct solution for deleting an array of objects. My suggestion is for when you want to delete a single element individually.

    I should of course have suggested that you should use "delete [] " to delete the array if that's what you want to do.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete Samllest Array Element
    By ptysell in forum C Programming
    Replies: 5
    Last Post: 11-22-2004, 06:27 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  4. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 04:50 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21