🔒 Locked
STL + Polymorphism?
Advertisement
Original Post
OK, I'm doing a little thinking and planning on my engine. I'm working on my memoery management system, and I'm trying to come up with the fastest and cleanest system I can come up with. What I have is a MemoryManagement singleton class. Two questions come up. MemoryManagement has a garbage collector that picks up unneeded memory objects. So I have a vector containing a stack of memory objects. Now I have a class called MemoryObject, which is only for inheritance, that is inherited by DMemoryO and SMemoryO. DMemoryO lets you add on memory when needed, where as SMemoryO, you create a set ammount of memory and that's it. I don't know the underlying system of the STL, so would I make put in: std::vector<MemoryObject> list; //or std::vector<MemoryObject*> list; ??? Which one would let me put in both static and dynamic memory objects? And if I used the second one, <MemoryObject*>, would I have the problem of having a 2d array?
Bumpy, anyone know????
If you have a class MemoryObject, which has two derived classes SMemoryObject and DMemoryObject, then to have a single array to hold both of them, you need this:
This is because derived classes can be referenced by a pointer of any of its base classes, the classes it was derived from. You can't have a vector of just "MemoryObject" because each of the derived classes could be a different size, and that would stuff everything, so, go with the "MemoryObject*".
Edit:: What do you mean about the 2nd array?
std::vector<MemoryObject*> list;This is because derived classes can be referenced by a pointer of any of its base classes, the classes it was derived from. You can't have a vector of just "MemoryObject" because each of the derived classes could be a different size, and that would stuff everything, so, go with the "MemoryObject*".
Edit:: What do you mean about the 2nd array?
Hmmm.... Are you sure? Doesn't vector contain a linked list with a pointer to the object? Would that do the trick or what? If not, then I would know what to do. Could I perhaps have a vector of references? Thank you!
Nope, std::vector isn't a linked list, it's a dynamic array. Also, it doesn't store a pointer to the type you specify, it stores instances of the complete type. So if you specifying MemoryObject it will store complete MemoryObjects, not pointers to them.
Quote:
Original post by joanusdmentia
Nope, std::vector isn't a linked list, it's a dynamic array. Also, it doesn't store a pointer to the type you specify, it stores instances of the complete type. So if you specifying MemoryObject it will store complete MemoryObjects, not pointers to them.
That's why if you want to store both DMemoryObject and SMemoryObject types in the same vector, you need to store MemoryObject* and create each DMemoryObject and SMemoryObject on the heap using 'new'.
Else, you could write a container that will keep several different types of different sizes, but then you'd end up with a lot of wasted space. But with the pointer being used, if later on you decide to create another type of memory object, you can just start storing it right away, because it too will be derived from MemoryObject, so you'll be able to access it with a MemoryObject*.
You can't use references because vector uses pointers and you can't legally have pointers to references or arrays of references (so VC++ 6 says).
Quote:
Original post by Endar Quote:
Original post by joanusdmentia
Nope, std::vector isn't a linked list, it's a dynamic array. Also, it doesn't store a pointer to the type you specify, it stores instances of the complete type. So if you specifying MemoryObject it will store complete MemoryObjects, not pointers to them.
That's why if you want to store both DMemoryObject and SMemoryObject types in the same vector, you need to store MemoryObject* and create each DMemoryObject and SMemoryObject on the heap using 'new'.
Else, you could write a container that will keep several different types of different sizes, but then you'd end up with a lot of wasted space. But with the pointer being used, if later on you decide to create another type of memory object, you can just start storing it right away, because it too will be derived from MemoryObject, so you'll be able to access it with a MemoryObject*.
You can't use references because vector uses pointers and you can't legally have pointers to references or arrays of references (so VC++ 6 says).
That last sentence disagrees with what joanusdmentia said... Sorry, I completely understand how the system works now, except that. Thanks.
Oh, ok thanks! You guys here at GameDev are the bomb!
A vector is a dynamic array. So, you'd have something roughly like this:
Something like that. What joanusdmentia was talking about is easier to understand if you think about a vector simply being an array in memory.
So, the class starts out with a pointer and creates an array. But, there's a pointer to the array, not a seperate pointer to every single element.
template <class T>class array{private: T* array; unsigned int size;public: array(unsigned int s) { array = new T[s]; size = s; }};Something like that. What joanusdmentia was talking about is easier to understand if you think about a vector simply being an array in memory.
MemoryObject* mem_array[50]; // instances of MemoryObject*int int_array[50]; // instances of intSo, the class starts out with a pointer and creates an array. But, there's a pointer to the array, not a seperate pointer to every single element.
Quote:
Original post by dbzprogrammer
Doesn't vector contain a linked list
See std::list
Quote:
with a pointer
See boost::ptr_vector
Quote:
Would that do the trick or what?
Well, there's a reason std::vector dosn't do this. One is preformance - this isn't to say it's the best solution even for preformance in all circumstances (far from - otherwise why would we have multiple container types?). But sometimes it is.
Another reason is to act like C arrays. And I mean really act like them. As in:
void old_function_expecting_a_c_array( char * data , size_t n ) { //use array[0..n-1]}int main () { std::vector< int > some_data( ... ); //fill some_data or specify starting data in some_data's ctor //since the C++ standard makes certain guarantees (such as that std::vector //will allocate all it's items directly next to each other) - this will work old_function_expecting_a_c_array( & some_data[0] , some_data.size() );}Quote:
If not, then I would know what to do. Could I perhaps have a vector of references?
Can't use C++ references (e.g. "int &") but you can use pointers ("int *") which are really just references... just with different syntax... and the ability to be nulled and reseated.
[Edited by - MaulingMonkey on October 30, 2005 12:26:53 AM]
That was awefully long. #1: For experience, I like knowing that I've done it before and how the system works. #2: I'm making a small C++ wrapper for the STL dude. That's it.
Quote:There are more important/useful things to learn than this low level hackery. Just...so many more compelling things to play with.
Original post by Kuladus Quote:
Original post by Promit
Suit yourself. But it's time wasted on unimportant things.
It's a learning exercise. Not a waste of time.
Quote:
Original post by Promit Quote:There are more important/useful things to learn than this low level hackery. Just...so many more compelling things to play with.
Original post by Kuladus Quote:
Original post by Promit
Suit yourself. But it's time wasted on unimportant things.
It's a learning exercise. Not a waste of time.
Basically your saying use the STL and keep track of whatever pointers you have? I already do that, so I thought making one of these would be good for a change. Plus it's easier to develop on.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement