G
Guest
Guest
Archived from groups: rec.games.roguelike.development (More info?)
When writing item handling code I encountered a problem that is
probably pretty common to object-oriented roguelikes.
For now my item handling classes include a generic Item class, and
Container, inheriting from it. Obviously I would like a Container
object to contain other items. For this purpose I'm using
std::list.
So my first idea was to do list<Item> l, or something similar.
A good c++ programmer (which I am not) will probably recognize
the problem that this causes - when I put a Container object
on such list, it gets reduced to an Item, all its methods being
substituted by ones from the Item class.
The easiest solution to this is using list<Item*>, but it brings
another problem, namely the destructor of std::list leaving
all the items in memory. I could delete them in the destructor
of the class that contains the list, but it wouldn't be too
elegant.
I could write a pointer_list<foo> class, which would use std::list
to store pointers to foo and delete them upon destruction. But
I would have to implement the numerous methods of std::list that
I'd like to use.
I concluded that I should make a pointer<foo> object that would
store a pointer to foo and have a destructor that deletes it.
But this brings a different problem (maybe less obvious), that
(for example) std::list<foo>:ush_back() takes the value of the
parameter, and thus uses the copying constructor. So we get two
"pointers" pointing at the same place, one of which might get
deleted after a while, thus freeing the memory that the other is
pointing to.
At this point I wanted to call for help on this group, but then
I had an idea to have a counter in each item that tells how many
"pointers" are pointing at it, destructors decreasing it, and
copying constructors increasing. When it comes to 0, the item
can be deleted.
So what do you guys think of this? Is the last solution optimal,
or are there others, which I haven't thought about?
Thanks for any help,
Michal Brzozowski
When writing item handling code I encountered a problem that is
probably pretty common to object-oriented roguelikes.
For now my item handling classes include a generic Item class, and
Container, inheriting from it. Obviously I would like a Container
object to contain other items. For this purpose I'm using
std::list.
So my first idea was to do list<Item> l, or something similar.
A good c++ programmer (which I am not) will probably recognize
the problem that this causes - when I put a Container object
on such list, it gets reduced to an Item, all its methods being
substituted by ones from the Item class.
The easiest solution to this is using list<Item*>, but it brings
another problem, namely the destructor of std::list leaving
all the items in memory. I could delete them in the destructor
of the class that contains the list, but it wouldn't be too
elegant.
I could write a pointer_list<foo> class, which would use std::list
to store pointers to foo and delete them upon destruction. But
I would have to implement the numerous methods of std::list that
I'd like to use.
I concluded that I should make a pointer<foo> object that would
store a pointer to foo and have a destructor that deletes it.
But this brings a different problem (maybe less obvious), that
(for example) std::list<foo>:ush_back() takes the value of the
parameter, and thus uses the copying constructor. So we get two
"pointers" pointing at the same place, one of which might get
deleted after a while, thus freeing the memory that the other is
pointing to.
At this point I wanted to call for help on this group, but then
I had an idea to have a counter in each item that tells how many
"pointers" are pointing at it, destructors decreasing it, and
copying constructors increasing. When it comes to 0, the item
can be deleted.
So what do you guys think of this? Is the last solution optimal,
or are there others, which I haven't thought about?
Thanks for any help,
Michal Brzozowski