473,782 Members | 2,542 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Designing a const-correct iterator hierarchy

This is an old problem of mine.

Basically I have an abstract base class which represents a generic iterator
over a collection of elements, and various derived classes that implement
the traversing on specific data structures.
For each iterator I want to be able to specify the four possible const
combinations, corresponding to the various [const] void* [const] for
pointers, with the possibility to perform conversions from non-const to
const just like you can do with pointers.
Also I obviously want the conversions from the specific iterators to the
generic base.

The first solution I employed was to create a hierarchy like
AbstractConstIt erator <-- AbstractIterato r <-- ConcreteIterato r.
Client code used just AbstractConstIt erator and AbstractIterato r, so that
all possible combinations of const and all conversions were possible.

Unfortunately the elements pointed by the iterators are themselves a
hierarchy, and in particular there is an AbstractElement class and some
derived ConcreteElement . Each ConcreteIterato r corresponds to a particular
ConcreteElement .
Also the various ConcreteElement classes extend the functionalities
available in the abstract base class with some new methods specific of the
type.
Since the client code sometimes needs to access those specific
functionalities , I had to make ConcreteIterato r externally visible and
provide the specific ConcreteElement s using covariance return.

In order to provide the four possible combinations of const and all the
required conversions, I changed the hierarchy this way:
- AbstractConstIt erator <-- AbstractIterato r
- AbstractConstIt erator <-- ConcreteConstIt erator
- AbstractIterato r, ConcreteConstIt erator <-- ConcreteIterato r

Then I made the two inheritances from AbstractConstIt erator virtual, in
order to avoid a double base class in ConcreteIterato r (among the other
things, to avoid problems when comparing pointers to AbstractConstIt erator
for equality).

Now the problem is that, while this design works correctly and provides the
functionalities I need, I don't like the fact that I'm using a run-time
facility (virtual inheritance) only to solve a compile-time problem
(const-correctness).

I thought this scenario were pretty common, but I haven't been able to find
previous discussions about it.

Do you have any suggestion?

--
Lorenzo Castelli
Feb 18 '06 #1
2 2013

Lorenzo Castelli wrote:
This is an old problem of mine.

Basically I have an abstract base class which represents a generic iterator
over a collection of elements, and various derived classes that implement
the traversing on specific data structures.
For each iterator I want to be able to specify the four possible const
combinations, corresponding to the various [const] void* [const] for
pointers, with the possibility to perform conversions from non-const to
const just like you can do with pointers.
Also I obviously want the conversions from the specific iterators to the
generic base.

The first solution I employed was to create a hierarchy like
AbstractConstIt erator <-- AbstractIterato r <-- ConcreteIterato r.
Client code used just AbstractConstIt erator and AbstractIterato r, so that
all possible combinations of const and all conversions were possible.

Unfortunately the elements pointed by the iterators are themselves a
hierarchy, and in particular there is an AbstractElement class and some
derived ConcreteElement . Each ConcreteIterato r corresponds to a particular
ConcreteElement .
Also the various ConcreteElement classes extend the functionalities
available in the abstract base class with some new methods specific of the
type.
Since the client code sometimes needs to access those specific
functionalities , I had to make ConcreteIterato r externally visible and
provide the specific ConcreteElement s using covariance return.

In order to provide the four possible combinations of const and all the
required conversions, I changed the hierarchy this way:
- AbstractConstIt erator <-- AbstractIterato r
- AbstractConstIt erator <-- ConcreteConstIt erator
- AbstractIterato r, ConcreteConstIt erator <-- ConcreteIterato r

Then I made the two inheritances from AbstractConstIt erator virtual, in
order to avoid a double base class in ConcreteIterato r (among the other
things, to avoid problems when comparing pointers to AbstractConstIt erator
for equality).

Now the problem is that, while this design works correctly and provides the
functionalities I need, I don't like the fact that I'm using a run-time
facility (virtual inheritance) only to solve a compile-time problem
(const-correctness).

I thought this scenario were pretty common, but I haven't been able to find
previous discussions about it.

Do you have any suggestion?


The obvious question to ask is why not model these iterators after one
of the five categories of iterators defined in the Standard Library? In
addition to the five iterator categories, the Standard defines both
constant and mutable iterators - depending on whether the referenced
value can be modified through the iterator. The Standard's constant and
mutable iterators seem to correspond quite closely to two of the
iterator types in your model. And these are the only two cases that
really need to be modeled explicitly. For iterator instances that are
themselves const, the "const" keyword applied to the iterator variable
declaration should be sufficient. In other words, two of your four
iterators can be modeled at the language level.

Using the Standard Library iterators (found <iterator>) as a model for
your own iterator classes would also address your primary complaint.
Because in order to implement a compile-time solution, it will be
necessary to forego an iterator model that relies on inheritance and
use one that is based on templates instead. The latter model inherently
describes runtime relationships, while the former inherently describes
relationships defined at compile-time. Therefore the Standard library
iterator model - which is implemented with templates - would provide a
compile-time solution.

Greg

Feb 19 '06 #2
"Greg" <gr****@pacbell .net> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
The obvious question to ask is why not model these iterators after one
of the five categories of iterators defined in the Standard Library? In
addition to the five iterator categories, the Standard defines both
constant and mutable iterators - depending on whether the referenced
value can be modified through the iterator. The Standard's constant and
mutable iterators seem to correspond quite closely to two of the
iterator types in your model. And these are the only two cases that
really need to be modeled explicitly. For iterator instances that are
themselves const, the "const" keyword applied to the iterator variable
declaration should be sufficient. In other words, two of your four
iterators can be modeled at the language level.

Using the Standard Library iterators (found <iterator>) as a model for
your own iterator classes would also address your primary complaint.
Because in order to implement a compile-time solution, it will be
necessary to forego an iterator model that relies on inheritance and
use one that is based on templates instead. The latter model inherently
describes runtime relationships, while the former inherently describes
relationships defined at compile-time. Therefore the Standard library
iterator model - which is implemented with templates - would provide a
compile-time solution.


The fact is that I need a "late-binding" solution. I probably didn't make
myself sufficiently clear.

I need the class hierarchy and the virtual functions because I need to
handle situations where the type of the iterator can be known only at
runtime.
For example I sometimes have containers of heterogeneous data structures,
and I have to implement algorithms that apply some function (that involves
traversing) to all the structures in the container. In this case virtual
functions are obviously required to select the right traversal algorithm for
each single structure in the container.
So I'm perfectly okay with virtual functions and I actually need that kind
of runtime support.

What I don't like too much is the fact the current design forces me to use
virtual inheritance too.
Since the iterators down in the hierarchy provide additional
functionalities , I have to make them publicly available. And each of them
should be able to be specified in all the four const combinations.

As you said I can use the const keyword to provide two combinations, so I'm
left to design only constant and mutable iterators.
Now the problem is with derived mutable iterators: this kind of iterators
should be able to be used in any situation a base mutable iterator is
required (so I derived from it), and also should be usable where a derived
constant iterator is required (so I derived from this class too). And this
is where multiple inheritance comes up, and consequently the use of virtual
inheritance.

Conversely suppose that I didn't care about const-correctness, and I just
provided support for mutable iterators.
In this case the hierarchy would be simply composed by
AbstractMutable Iterator <-- DerivedMutableI terator. No need for multiple
(and virtual) inheritance.

So here is my problem: in my design I had to add virtual inheritance just to
provide support for const-correctness. A runtime facility for a compile-time
feature.
That's why I don't like my design too much and I'm wondering if there exists
alternative solutions.

--
Lorenzo Castelli
Feb 19 '06 #3

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

Similar topics

6
2142
by: E G | last post by:
Hi! I am having problems in designing a class. First, I have a base class that allocates a 3D data set and allows some other mathematical operations with it, something like this: template <typename T> class BasicArray {
12
1892
by: James Brown | last post by:
Hi all, Having problems designing a template-class. I'll describe my scenario first then show what I've come up with so far: Need a class to provide pointer/array-like access to an area of physical memory located on a piece of custom hardware - this memory is only accessible using machine specific i/o so I want to hide all this in a class. I'm imagining
2
1296
by: James Brown | last post by:
Hi again, I'm referring back to my previous posting of the same title, with everyone's help I've now got a better understanding of what my goals are and I have a class which now looks like: template <typename type> class PhysMem { public:
13
16736
by: Charulatha Kalluri | last post by:
Hi, I'm implementing a Matrix class, as part of a project. This is the interface I've designed: class Matrix( )
5
2639
by: Steve Edwards | last post by:
Hi, With much help from this forum I've been using stl containers, but always with other common - or stl - types as members. I've now run in to a problem while trying to use some of my own structures: enum MySym{symA, symB, symC, symD ... etc.} typedef struct{ MySym s1, s2, s3;
16
4198
by: C++ Hell | last post by:
Hey everyone just designing a quiz for school and managed to write the code for the questions and answers thou i have to add scores and at the end an overall score does anyone have any idea what to do or what i am talkin about
3
1897
by: mystilleef | last post by:
Hello, I need to design a plug-in system for a project. The goal is to allow third party developers interact with an application via plug-ins in a clean and robust manner. At this point I am overwhelmed by my inexperience with designing plug-in systems. Are there any good tutorials on how to design good plug-in systems with Python, or any language? What are the best
0
1987
by: Memfis | last post by:
While I was looking through the group's archives I came across a discussion on doing properties (as known from Delphi/C#/etc) in C++. It inspired me to do some experimenting. Here's what I came up with. First we have an interface defining how a property can be used (read and written - for simplicity, I ignored read-only and write-only variants). template<typename Tclass Property { public:
8
2193
by: pransri2006 | last post by:
Hi guys! I think all of u know about the designing of compilers. Can any body tell me about the designing of the compilers. And also tell me the difference between the compilers and Interpreter which weresically used by the computers. I want to know about the basic compiler software.
2
1561
by: e2point | last post by:
i need to create a storage module which can store any thing the user wants. I want it to handle almost any storage requirement, so that clients can define their own key classes derived from the base key class which is defined by the storage module itself. for example: in the storage module we have class base_key { public:
0
9639
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7492
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.