+ Reply to Thread
Results 1 to 7 of 7

Thread: Storing a Class in a Vector

  1. #1
    Learning Programmer Buttacup is an unknown quantity at this point
    Join Date
    Jun 2009
    Location
    Vancouver, BC
    Age
    30
    Posts
    61

    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. #2
    Programmer Mathematix is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    104

    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.

  3. #3
    Learning Programmer Buttacup is an unknown quantity at this point
    Join Date
    Jun 2009
    Location
    Vancouver, BC
    Age
    30
    Posts
    61

    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 07:28 AM. Reason: add code tags (the # button)

  4. #4
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,662
    Blog Entries
    57

    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.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #5
    Learning Programmer Buttacup is an unknown quantity at this point
    Join Date
    Jun 2009
    Location
    Vancouver, BC
    Age
    30
    Posts
    61

    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.

  6. #6
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,662
    Blog Entries
    57

    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.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  7. #7
    Learning Programmer Buttacup is an unknown quantity at this point
    Join Date
    Jun 2009
    Location
    Vancouver, BC
    Age
    30
    Posts
    61

    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 04:48 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Tutorial: Starting C# with C# 2008 Express Edition
    By Jordan in forum CSharp Tutorials
    Replies: 20
    Last Post: 07-27-2009, 04:45 AM
  2. PHP Objects
    By chili5 in forum PHP Tutorials
    Replies: 5
    Last Post: 03-24-2009, 05:12 PM
  3. Vectors
    By chili5 in forum Java Tutorials
    Replies: 10
    Last Post: 02-17-2009, 06:55 PM
  4. PHP 5 and OOP
    By Jordan in forum PHP Tutorials
    Replies: 11
    Last Post: 09-22-2008, 01:58 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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