Need help with a container design

M

Michael Klatt

I need to design a container similar to a std::set, but where the
stored objects may be modified via a non-const iterator. In some
cases the modification will effect the sort order.

Here's an example:

class Object { // an object from the database };

class Sorter { // functor that determines sort order for Objects };

template <class Obj, class Sort> class Set {};

Set<Object, Sorter> query;
Set<Object, Sorter>::Iterator it(query.begin());
while (it != query.end())
{
it->modify(); // uh oh, Set<> may be invalid now
}

In addition to this basic concept, is there a way to design Set<> so
that it automagically knows when a contained object has been changed
in a way that requires the set to be resorted?
 
R

Ron Natalie

Michael Klatt said:
In addition to this basic concept, is there a way to design Set<> so
that it automagically knows when a contained object has been changed
in a way that requires the set to be resorted?

There's no way to tell. A container can't tell an arbitrary internal object has been
changed. There's nothing that says you can't get a reference or pointer to an
object from an iterator and release, increment, or otherwise destroy the interator.
 
D

David Harmon

I need to design a container similar to a std::set, but where the
stored objects may be modified via a non-const iterator. In some
cases the modification will effect the sort order.

My approach would be to use a std::set, and whenever you modify a stored
object you obtain a copy, erase the stored object, modify the copy, and
insert it back in the set. Encapsulate in a suitable class wrapper.
 
L

lilburne

Michael said:
I need to design a container similar to a std::set, but where the
stored objects may be modified via a non-const iterator. In some
cases the modification will effect the sort order.

I wouldn't modify the values in a set without first removing
the old item. What if you modify the item to be the same as
some other item? What if the implementation locates items
based on the sort order?
 
J

Jumbo

Michael Klatt said:
I need to design a container similar to a std::set, but where the
stored objects may be modified via a non-const iterator. In some
cases the modification will effect the sort order.

Here's an example:

class Object { // an object from the database };

class Sorter { // functor that determines sort order for Objects };

template <class Obj, class Sort> class Set {};

Set<Object, Sorter> query;
Set<Object, Sorter>::Iterator it(query.begin());
while (it != query.end())
{
it->modify(); // uh oh, Set<> may be invalid now
}

I'm not sure what you mean by invalidating it. You mean calling it's own
destructor or something drastic ? If so, well yes anything could happen. But
this is not down to bad class design as you can screw up a program anyplace
by deleting an object when your not supposed to.
In addition to this basic concept, is there a way to design Set<> so
that it automagically knows when a contained object has been changed
in a way that requires the set to be resorted?

Depends what you mean by contained object.
Perhaps you mean a Base class or a nested class. Or perhaps simply member
data?
But in general I believe your idea could work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top