473,769 Members | 2,240 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheriting interators

I am trying to define a series of iterators for a container I am
building, and as forward, bidirectional and random access iterators are
very similar I thought it would be handy to put them into a heirachy,
much like the code below:

struct MyForwardIterat or {
typedef forward_iterato r_tag iterator_catego ry;
// forward iterator functions
};

struct MyBidirIterator : public MyForwardIterat or {
typedef bidirectional_i terator_tag iterator_catego ry;
// extra bidir functions
};

struct MyRandomAccessI terator : public MyBidirIterator {
typedef random_access_i terator_tag iterator_catego ry;
//extra random access functions
};

While this appears to work fine, I feel a little nervous about
overloading typedefs in the different classes. Should I be worried and
is there any unpleasent errors that could arise from this kind of thing?

PS: I know that if I can provide a random access iterator then I don't
have to provide bidirectional and forward iterators
Jul 22 '05 #1
5 1509

"Azumanga" <az******@bubbl escope.net> wrote in message
news:cf******** **@pump1.york.a c.uk...
I am trying to define a series of iterators for a container I am
building, and as forward, bidirectional and random access iterators are very similar I thought it would be handy to put them into a heirachy, much like the code below:

struct MyForwardIterat or {
typedef forward_iterato r_tag iterator_catego ry;
// forward iterator functions
};

struct MyBidirIterator : public MyForwardIterat or {
typedef bidirectional_i terator_tag iterator_catego ry;
// extra bidir functions
};
<snip>
While this appears to work fine, I feel a little nervous about
overloading typedefs in the different classes. Should I be worried and is there any unpleasent errors that could arise from this kind of

thing?

I wouldn't worry about hiding the typedefs from the base classes since
users will see the types of the iterators as container::iter ator and
won't know about the base classes.

I would worry about writing conforming iterators, however, since it's
tricky. I'd recommend at least looking at the boost iterator library
before writing iterators from scratch.
(http://www.boost.org/libs/iterator/doc/index.html)

Jonathan
Jul 22 '05 #2
"Azumanga" <az******@bubbl escope.net> wrote in message
I am trying to define a series of iterators for a container I am
building, and as forward, bidirectional and random access iterators are
very similar I thought it would be handy to put them into a heirachy,
much like the code below:

struct MyForwardIterat or {
typedef forward_iterato r_tag iterator_catego ry;
// forward iterator functions
};

struct MyBidirIterator : public MyForwardIterat or {
typedef bidirectional_i terator_tag iterator_catego ry;
// extra bidir functions
};

struct MyRandomAccessI terator : public MyBidirIterator {
typedef random_access_i terator_tag iterator_catego ry;
//extra random access functions
};

While this appears to work fine, I feel a little nervous about
overloading typedefs in the different classes. Should I be worried and
is there any unpleasent errors that could arise from this kind of thing?


It seems OK to me to override static functions and types (ie. nested classes
and typedefs).

The problem with overriding non-virtual member functions is that when you
call the function from a pointer to the base class or pointer to the derived
class you get different behavior, which is not intuitive. In reality, the
same can be said for static functions and types.

(Though as an aside I think it's OK to override non-virtual functions if you
return the same object in a covariant manner, so the base class returns a
std::auto_ptr<B ase> and the derived class returns the same object as a
std::auto_ptr<D erived>).

However, because C++ is a statically typed language, the argument does not
hold much sway for static types. Sure, the derived class may point to a
different type. But when you write

template <class Container>
typename Container::valu e_type product(const Container& c) {
if (c.empty()) throw ContainerHasZer oSize("product" );
typename Container::valu e_type result = c.first();
typedef typename Container::cons t_iterator Iter;
Iter iter = c.begin();
const Iter end = c.end();
for (++iter; iter!=end; ++iter) result *= *iter;
return result;
}

the template function 'product' will not be part of the compiled object file
or executable. It is only when you call product(const std::vector<int >&)
that the compiler instantiates the template function 'product' for that
particular type (namely Container=std:: vector<int>), and the specialization
becomes part of the compiled object file.

Now at compile time we know the exact type of Container, so we can just call
the template function product(const Container&), and the compiler will
instantiate the function for us at compile time, using the correct semantics
of Container, Container::valu e_type, Container::cons t_iterator, and so on.
It will use the correct sizeof, substitute the correct operator*, and so
forth.

Had C++ been a dynamically typed language, with template instantiation
occuring at runtime after we instantiated a particular Container, then
having virtual static types (ie. nested classes and typedefs) would make
sense.

Does this make sense?
Jul 22 '05 #3
Azumanga <az******@bubbl escope.net> wrote:
PS: I know that if I can provide a random access iterator then I don't
have to provide bidirectional and forward iterators


I would put it quite differently: if you can provide random access
iterators for a sequence, it is quite useless to also provide iterators
for this sequence which are more restrictive! In fact, for some
algorithms taking less powerful iterators it would actually be harmful!
Just because e.g. 'copy()' operates on input and output iterators it
doesn't mean that it can't benefit from random access iterators.

Always provide the most powerful iterator for your sequences as possible.
Do not provide less powerful ones because all this achieves is preventing
possibilities for optimizations.
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting
Jul 22 '05 #4
> Always provide the most powerful iterator for your sequences as possible.
Do not provide less powerful ones because all this achieves is preventing
possibilities for optimizations.


I entirely agree with you, however the purpose of my creating these
iterators is exactly to test algorithms on multiple types of iterators
exactly to find out what optimisations are possible and performed :)

Chris
Jul 22 '05 #5
Siemel Naran wrote:
<snip>
Does this make sense?


Yes it does, and it sets my mind at rest. Thanks :)
Jul 22 '05 #6

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

Similar topics

1
1878
by: richardlane | last post by:
Hi, basic question here - I'm struggling with the transfer from asp to asp.net a bit, especially in seeing the 'bigger picture' of how things are best structured. I have a website made up primarily of aspx pages. I have separated out the common <head> as an .ascx file and intend to do the same for other common regions of html. In the aspx.vb I have code that is common to all pages. For example a sub which is passed a parameter to tell...
15
1713
by: JustSomeGuy | last post by:
this doesn't want to compile.... class image : public std::list<element> { element getElement(key k) const { image::iterator iter; for (iter=begin(); iter != end(); ++iter) { element &elem(*iter);
29
5901
by: shaun roe | last post by:
I want something which is very like a bitset<64> but with a couple of extra functions: set/get the two 32 bit words, and conversion to unsigned long long. I can do this easily by inheriting from bitset<64>, but I know that STL has no virtual destructor. Can I get around this by calling the baseclass destructor explicitly in my derived class? Is there another way to get all of the bitset<64> functionality without rewriting a lot of...
1
2447
by: john diss | last post by:
hello there everyone.. I have created a class called "ProcessLog" inheriting from XmlDocument and two classes ("UploadedItem", "ProcessError") inheriting from XmlElement. I then have two classes ("UploadedTemplate", "UploadedPresentation") which inherit from "UploadedItem")... so far so good. I have overriden the CreateElement method of "ProccessLog" and it creates the correct elements as expected. The problem is I need to override...
2
2235
by: Peter Bates | last post by:
Hi, I'm just getting used to XSDObjectGen and i have the following question. Can i use a class inherited from a class generated by XSDObjectGen with XmlSerialize? Specifically, I have many xml files arriving from a PC inventory scanner we use. I wish to deserialize them and then process them.
11
2168
by: Noah Coad [MVP .NET/C#] | last post by:
How do you make a member of a class mandatory to override with a _new_ definition? For example, when inheriting from System.Collections.CollectionBase, you are required to implement certain methods, such as public void Add(MyClass c). How can I enforce the same behavior (of requiring to implement a member with a new return type in an inherited class) in the master class (similar to the CollectionBase)? I have a class called...
0
1353
by: z. f. | last post by:
Hi, I'm not so familiar with control inheriting, but what i need is very simple (should be) and requires only deep knowledge with implementing control inheritence. is it possible (might someone post a sample / link to sample) to implement a control inheriting from the repeater / date grid / data list, that as well as implementing those control properties (HTML-side properties) implements also a <no items template> that will accept...
2
4045
by: Shayne H | last post by:
I wanted to create a type-safe collection by inheriting from CollectionBase. Public Class MyCollection : Inherits CollectionBase I found that IList.Add did not allow the setting of a "key" to identify an object added to the collection. So to implement a "key" similar to the parameter allowed by the standard Collection object do I have to create my own array to store the "keys" and manage it within my class MyCollection? I can't see how...
2
1813
by: Charles Law | last post by:
I want a set of controls that all have a border, like a group box. I thought I would create a base control containing just a group box from which my set of controls could inherit. Assuming that this is the right approach (please tell me if it is not), how then do I make it so that the group box cannot be moved around on my set of controls, but is also able to act as container for other controls? If I leave the modifier of the group box...
3
5385
by: Alex Satrapa | last post by:
There's some mention in the (old!) documentation that constraints such as foreign keys won't include data from inheriting tables, eg: CREATE TABLE foo ( id SERIAL PRIMARY KEY ); CREATE TABLE bar ( attribute integer NOT NULL ) INHERITS (foo);
0
9590
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
9424
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
10051
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
7413
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
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
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...
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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.