CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2007
    Posts
    3

    array of class instances

    I have an array of instances of a class. I initialize a limit of 5 because i want a maximum of 5 of them at one time. I want to be able to, when a new one is to be created, store it to the array in the first avaible spot. The problem is I don't have much experience in that regard. So the part I need help with is:
    1) do I store the instance to the array, or a pointer of it, or does it matter.
    2) how do I check to see which one is the first empty, I'm thinking using a while loop, but I don't know what it would be checked against

    I think that covers it, I appreciate your help, and if anymore information is needed I will provide.

  2. #2
    Join Date
    Jun 2001
    Posts
    104

    Re: array of class instances

    You could do it either way, with pointers or instances, although instances isn't quite the proper term. What you're doing is filling up an array of memory slots for your structure. If your class structures are large, then you might prefer pointers to save memory.

    To determine if a slot is empty, you can set one of the parameters in the structure to zero, or you could include an extra structure, which would hold a variable to indicate if a slot is empty. With pointers you could just set the pointers to NULL if the slot is empty.

    The instance, or structure approach:

    Code:
    class myClass{
        int var1;
        int empty;
    };
    
    myClass array[5], zero;
    zero.var1 = 0;
    zero.empty = 0;
    
    //initialize
    for(int i=0;i<5;i++){
      array[i] = zero;
    }
    
    //add entry
    for(int i=0; i< 5;i++){
      if(array[i].empty == 0){
          array[i].var1 = 12;
          array[i].empty = 1;
          break;
      }
    }
    Then, when you want to add a value, you iterate through each slot until you find an empty one and stick your stucture in.


    Using pointers
    Code:
    myClass *pArray[5];
    
    //initialize
    for(int i=0;i<5;i++){
       pArray[i] = NULL;
    }
    
    //Then, to add a value
    for(int i = 0; i< 5; i++){
       if(pArray[i] == NULL){
            pArray[i] = new myClass();
            pArray[i]->var1 = 10;    
           break;
       }
    }
    In this case you will have to delete each element in the array when you're done with them to prevent a memory leak.

    Hope that's clear.

    You can also rate my post if it was helpful. Thanks.

  3. #3
    Join Date
    Jun 2007
    Location
    United States
    Posts
    275

    Re: array of class instances

    If you are going to be creating/deleting instances, it would be easier if you held then in a vector. If you dont know, a vector is basicly an array with a lot of cool functions, like add, delete, insert, ect.

    http://www.cppreference.com/cppvector/index.html

    They can be accessed with [] just like an array, or iterators if u want to do it more efficiently.
    Last edited by bovinedragon; October 12th, 2007 at 09:30 PM.
    Don't forget to rate my post if I was helpful!

    Use code tags when posting code!
    &#091;code] "your code here" [/code]

  4. #4
    Join Date
    Oct 2007
    Posts
    3

    Re: array of class instances

    Thanks to both of you for your time.
    I will address jalway first, thank you for correctly my vocabulary, although I was aware of that to begin with, I typed the topic up very quickly because I started to become frustrated with my attempts and just wanted an answer as quick as possible. I think I will use the pointer method, but I forgot how to manage it properly, like if I'm done with the class how do I get rid of it, for lack of a better term, so that there isn't a memory leak. Also, that was more or less the approach I was trying, I was forgetting to store the pointer and setting them to NULL first. I might have a bit more problems when I go to actually do it though, because I'm new to c++ in the sense of I know the syntax and howto's for the most part but havent had much implementation practice.
    Now for bovinedragon, thanks for that information, I believe a vector, which i didn't know what it was before, is the best fit but I think its best if I go to them with a little more experience, if you don't agree, you probably know better.
    Again, thanks for the help in a matter which is probably very simple, I will rate both of your posts soon, when i get the time, as they were extremely helpful.

  5. #5
    Join Date
    Jun 2001
    Posts
    104

    Re: array of class instances

    Quote Originally Posted by pyrotechnic
    Thanks to both of you for your time.
    I will address jalway first, thank you for correctly my vocabulary, although I was aware of that to begin with, I typed the topic up very quickly because I started to become frustrated with my attempts and just wanted an answer as quick as possible. I think I will use the pointer method, but I forgot how to manage it properly, like if I'm done with the class how do I get rid of it, for lack of a better term, so that there isn't a memory leak.
    Well, after you create an array of pointers to object instances and you're done using them, you want to delete them.

    To delete them you just iterate through the array and delete one after the other like this.

    Code:
    for(int i=0;i<5;i++){
       if(pArray[i] != NULL){
            delete pArray[i];
            pArray[i] = NULL;
       }
    }

  6. #6
    Join Date
    Oct 2007
    Posts
    3

    Re: array of class instances

    Again, thanks. I believe that covers everything I needed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured