Thread: Array of Objects

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    5

    Array of Objects

    Hi,
    I was wondering if it is possible to delete an element from an array of objects. If so, can anyone give me a few pointers on how? I don't think using delete[] would work in this case?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Arrays are of fixed length. You can nullify the data for a given array element, but you can't, per se, "delete" an array element once an array is created of a given size.

    In C++ there are also vectors, and they can be of varying size and changed dynamically when a program runs.

    Todd

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    5
    Ok... Seems solid, what about trying something along the lines of:

    Code:
    bool deleteEntry (string nameToDelete)
    {
        for (int i = 0; i < numEntries; i++)
        {
            Telephone *b = NULL;
            
            if (nameToDelete == entries[i].getName())
            {
                b = new Telephone (entries[i]);
                return true;
            }
                
        }   
        return false;
    }
    ...but it appears to do nothing. I apologize for taking up your time with these questions, I'm fairly new to programming.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    There's a lot of information not there.

    We don't know what kind of container entries[] is.

    If the purpose of the function is to delete an entry, for one thing, it's not deleting anything. Rather, it actually acquires a new instance of a Telephone object, but then does nothing with it (that is apparent from this code fragment).

    What's your question?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you expect it to do anything? You copy the telephone entry you want to delete to another pointer. Why would that delete the first one? (Granted, the second one gets deleted when it goes out of scope when the function ends, but that doesn't take the first one with it.)

    If entries is a vector, you can use erase. If entries is an array, then entries[i] will always and forever exist (as long as the array does), but I suppose you can blank out its contents using ... well, whatever it is you use to change things in Telephone.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    5
    entries[] is an array of telephones. Each element in the array contains a person's name + area code + telephone number. The idea is to nullify the data for a given element as you said but I'm not sure how to go about that.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you want to do it manually, then you'll need to copy each entry beyond the one that you found down by one position. And decrement numEntries.

    If you used a vector instead of array, it might be easier to use the alternative: algorithms + vector::erase.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    5
    Quote Originally Posted by anon View Post
    If you want to do it manually, then you'll need to copy each entry beyond the one that you found down by one position. And decrement numEntries.

    If you used a vector instead of array, it might be easier to use the alternative: algorithms + vector::erase.
    The first solution is more along the lines of what I was looking for. Thank you.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by tabstop View Post
    (Granted, the second one gets deleted when it goes out of scope when the function ends
    No, it doesn't. The object is leaked.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    You may want to use a linked list instead of a vector/array if you wish to frequently delete objects in the middle without keeping a bunch of nullified objects in the array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Adding objects to an array
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2001, 09:24 AM