Skip to main content
gamedev.net
🔒 Locked

STL + Polymorphism?

Started by dbzprogrammer 16 replies 800+ views
Original Post
dbzprogrammer
dbzprogrammer
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?
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
dbzprogrammer
dbzprogrammer
Bumpy, anyone know????
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Endar
Endar
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:
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?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
dbzprogrammer
dbzprogrammer
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!
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
joanusdmentia
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.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o
Endar
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).
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
dbzprogrammer
dbzprogrammer
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.

We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Sneftel
Sneftel
No, it doesn't disagree. Internally, std::vector uses a single pointer to refer to the area of memory containing the elements. Those elements, in turn, are the actual elements, not pointers to elements.
dbzprogrammer
dbzprogrammer
Oh, ok thanks! You guys here at GameDev are the bomb!
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Endar
Endar
A vector is a dynamic array. So, you'd have something roughly like this:

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 int

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.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
MaulingMonkey
MaulingMonkey
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]
Promit
Promit
That was me. Silly GDNet. And I should point out that you've already encountered difficulties in this wholly pointless venture, without having even started to code.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
dbzprogrammer
dbzprogrammer
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.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Promit
Promit
Suit yourself. But it's time wasted on unimportant things.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Kuladus
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.
Promit
Promit
Quote:
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.
There are more important/useful things to learn than this low level hackery. Just...so many more compelling things to play with.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
dbzprogrammer
dbzprogrammer
Quote:
Original post by Promit
Quote:
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.
There are more important/useful things to learn than this low level hackery. Just...so many more compelling things to play with.


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.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.