[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Using std::set but need to change an element
- To: linuxgames@xxxxxxxxxx
- Subject: Using std::set but need to change an element
- From: Gianfranco Berardi <gberardi@xxxxxxxxxxx>
- Date: Tue, 01 Aug 2006 22:21:57 -0500
- Delivered-to: archiver@seul.org
- Delivered-to: mailing list linuxgames@sunsite.dk
- Delivery-date: Tue, 01 Aug 2006 23:22:14 -0400
- Mailing-list: contact linuxgames-help@sunsite.dk; run by ezmlm
- Reply-to: linuxgames@xxxxxxxxxx
- User-agent: Debian Thunderbird 1.0.7 (X11/20051017)
I found some code at http://www.gphysics.com to do "quick and simple"
physics, but I find that the code was written in VC++. I've managed to
get it to mostly compile in g++ 4.0.4, but I'm having trouble with the
use of std::set.
The code defines an Arbiter as the decision-maker between two Body
objects. It keeps the Arbiter objects in std::set<Arbiter> arbiters.
It can find out if an arbiter already exists in the set, but then the
code wants to run some operations on each arbiter. None of the
operations are const, and they can't because they change the members.
The problem is that the ArbIter is a typdef to set<Arbiter>::iterator,
which in g++ just ends up being a const_iterator anyway. It makes sense
since a set is supposed to have immutable members.
Then I found
http://www.velocityreviews.com/forums/t287950-which-type-should-quotstdsetbegin-constquot-return.html
which seems to suggest that iterator shouldn't be implemented as
const_iterator. Is g++ wrong and VC++ correct in this regard?
If not, how would I do something like the following:
for (ArbIter arb = arbiters.begin(); arb != arbiters.end(); ++arb)
{
(*arb).PreStep(inv_dt);
}
Do I really need to change Arbiter's members to mutable? I was thinking
about erasing the arbiter from the set, using const_cast, and then
inserting it again, which should be fine except that using const_cast
makes me nervous. I figure there might be a better solution that is
currently escaping me. Then again, it only makes me nervous because
I've never used it and I always read that I shouldn't. Perhaps this is
one of those situations when its use is valid?
Thank you for your time,
Gianfranco
--
GBGames' Blog, An Indie Game Developer's Somewhat Interesting Thoughts:
http://www.gbgames.com/blog
Staff Reviewer for Game Tunnel, the Independent Underground:
http://www.gametunnel.com