[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Using std::set but need to change an element



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