473,396 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Need help with a container design

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?
Jul 22 '05 #1
4 1333

"Michael Klatt" <md*****@ou.edu> wrote in message news:2c**************************@posting.google.c om...
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.
Jul 22 '05 #2
On 18 Jan 2004 15:15:41 -0800 in comp.lang.c++, md*****@ou.edu (Michael
Klatt) was alleged to have written:
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.

Jul 22 '05 #3
Michael Klatt wrote:
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?

Jul 22 '05 #4

"Michael Klatt" <md*****@ou.edu> wrote in message
news:2c**************************@posting.google.c om...
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.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Soulshift | last post by:
Hi all, Here's my problem. I have a design where the class Container inherits class Widget, and is used to contain and handle the proper display of widgets and behaves like a widget itself. I...
4
by: Matt Stanley | last post by:
I am trying to build a page that scales to fit the browser window regardless of size or resolution. The navigation on the top of the page is framed in dark red/brown using CSS with a background...
2
by: Jackson Yap | last post by:
can someone kind enough to help me look at the attached html and js file? Why is it that the javascript menu could not work at www.apchosting.net but could work at...
7
by: Jack Addington | last post by:
I've got a fairly simple application implementation that over time is going to get a lot bigger. I'm really trying to implement it in a way that will facilitate the growth. I am first writing a...
12
by: Raed Sawalha | last post by:
I have the following table which i can not switcha to design view error message said Could not open in Design view. Quote values differently inside a '<% ...."value"... %>' block. and the...
3
by: Tom | last post by:
This is probably pretty easy, but how does one determine if a control is a CONTAINER control? I thought something like If Typeof myControl is ContainerControl then .... would work but that...
1
by: Miguel Dias Moura | last post by:
Hello, I have a GridView in my page which is created in runtime. It works fine. My page has 2 Asp Buttons: - The HIDE button makes GridView.Visible = False; - The SHOW button makes...
18
by: bsruth | last post by:
I tried for an hour to find some reference to concrete information on why this particular inheritance implementation is a bad idea, but couldn't. So I'm sorry if this has been answered before....
0
by: gerry | last post by:
I am trying to create a custom container control that will only ever contain a specific type of control. At design time, when a control of a different type is added to the container I would like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.