Go Back   CodeCall Programming Forum > Software Development > C and C++
Register Blogs Search Today's Posts Mark Forums Read

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-03-2009, 10:46 PM
Buttacup's Avatar
Learning Programmer
 
Join Date: Jun 2009
Posts: 52
Buttacup is an unknown quantity at this point
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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-03-2009, 11:23 PM
Programmer
 
Join Date: Jun 2009
Posts: 104
Mathematix is an unknown quantity at this point
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-03-2009, 11:43 PM
Buttacup's Avatar
Learning Programmer
 
Join Date: Jun 2009
Posts: 52
Buttacup is an unknown quantity at this point
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 08:28 AM.. Reason: add code tags (the # button)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-04-2009, 08:31 AM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-04-2009, 04:28 PM
Buttacup's Avatar
Learning Programmer
 
Join Date: Jun 2009
Posts: 52
Buttacup is an unknown quantity at this point
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 07-04-2009, 04:53 PM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 07-04-2009, 05:06 PM
Buttacup's Avatar
Learning Programmer
 
Join Date: Jun 2009
Posts: 52
Buttacup is an unknown quantity at this point
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 05:48 PM..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial: Starting C# with C# 2008 Express Edition Jordan CSharp Tutorials 20 07-27-2009 05:45 AM
PHP Objects chili5 PHP Tutorials 5 03-24-2009 06:12 PM
Java Vectors chili5 Java Tutorials 10 02-17-2009 07:55 PM
PHP 5 and OOP Jordan PHP Tutorials 11 09-22-2008 02:58 AM


All times are GMT -5. The time now is 06:55 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0