Closed Thread
Results 1 to 7 of 7

Thread: Storing a Class in a Vector

  1. #1
    Buttacup's Avatar
    Buttacup is offline Learning Programmer
    Join Date
    Jun 2009
    Location
    Vancouver, BC
    Posts
    76
    Rep Power
    0

    Storing a Class in a Vector

    How bad is this really?? How much better of an idea is it to store a pointer as opposed to the class itself?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Mathematix is offline Programmer
    Join Date
    Jun 2009
    Posts
    112
    Rep Power
    0

    Re: Storing a Class in a Vector

    It's an extremely bad idea to do it both ways, because the vector could become too difficult to keep trace of as your code grows! And if there is a nasty bug preventing your game from functioning correctly, you could end up with a very hard-to-find bug or two.

    It sounds to me that you need to look into 'Design Patterns'. These provide advanced methods for structuring your data in a safe way, and managing how many, as well as when instances of your class will be spawned. Research 'singleton' and 'factory' as the most popular examples.

  4. #3
    Buttacup's Avatar
    Buttacup is offline Learning Programmer
    Join Date
    Jun 2009
    Location
    Vancouver, BC
    Posts
    76
    Rep Power
    0

    Thumbs up Re: Storing a Class in a Vector

    I'm not so sure I need a Class Factory for the purpose intended here. I have simply created a Class to contain the parts of the body of the components of the .x file as a temporary means of storage

    skipping most of all the code............
    Code:
    class CSceneObject
    {
    public:
         string name;
         CSeneObject(string str) : name(str) {};
         vector<string> vBody;
    };
    
    class other
    {
    private:
         vector<CSceneObject> vObjects;
    };
    The components will soon after be dissected and stored in containers held by the DX interfaces......... I did look into factories before hand, but I disregarded. I believe factories would pertain more to a large scale creation of objects............yes/no??
    Last edited by WingedPanther; 07-04-2009 at 05:28 AM. Reason: add code tags (the # button)

  5. #4
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: Storing a Class in a Vector

    It depends a LOT on the class. Vectors occassionally need to reallocate their memory, which means moving ALL contents to the new, larger, internal array. If you have a class with a lot of data, this can make resizing the vector very expensive unless you use pointers to the class in your vector. Unless you're using some form of smart pointer, however, you now have the potential for memory leaks.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    Buttacup's Avatar
    Buttacup is offline Learning Programmer
    Join Date
    Jun 2009
    Location
    Vancouver, BC
    Posts
    76
    Rep Power
    0

    Re: Storing a Class in a Vector

    But reallocation doesn't occur every time you add a new element to the vector??

    I think my hang up here is I don't want an array of anything like pointer[i]. I want to use some form of unique identifier, a name, a UUID. The way I set it up above is good for me but I'm already seeing that it takes for ever to process.

  7. #6
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: Storing a Class in a Vector

    It depends on your implementation, the particular class, etc. Also, are you storing a copy of an existing class, a reference to an existing class, etc? If the copy constructor is having issues, that would explain a LOT.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  8. #7
    Buttacup's Avatar
    Buttacup is offline Learning Programmer
    Join Date
    Jun 2009
    Location
    Vancouver, BC
    Posts
    76
    Rep Power
    0

    Re: Storing a Class in a Vector

    Code:
    vObjects.push_back(CSceneObject(vMeshFile.front()));
    
    while(!(vMeshFile.front().find("}") == 1))
    {
         vObjects.back().vBody.push_back(vMeshFile.front());
         vMeshFile.erase(vMeshFile.begin());
    		
         if(vMeshFile.size() == 1) 
               return false;
    }
    that's about it.........

    I should probably stop cutting and pasting everything from and to containers. It would probably be best to set pointers to various parts of the file(index it) and then copy the appropriate sections to the appropriate containers within the DX interfaces........
    Last edited by Buttacup; 07-04-2009 at 02:48 PM.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 10-21-2011, 08:21 AM
  2. Replies: 2
    Last Post: 03-30-2011, 07:13 PM
  3. Replies: 4
    Last Post: 06-30-2010, 09:22 PM
  4. Replies: 2
    Last Post: 07-10-2009, 11:36 AM
  5. Replies: 2
    Last Post: 11-11-2008, 08:31 AM

Tags for this Thread

Bookmarks

Posting Permissions

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