473,386 Members | 1,873 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,386 software developers and data experts.

Questions about Getting const_iterator out of iterator class

If I were to design my own container and its iterators, I understand
that I need to provide both "iterator" and "const_iterator" so that
begin() and end() return appropriate ones depending on the
"constantness" of the container object. I also note that is it done
through overloading begin() and end() like below:

iterator begin();
const_iterator begin() const;

So it seems to me that I need to have two separate iterator classes
which are very similar except that const_iterator's operator*()
returns const reference. I also need to provide conversion from
iterator to const_iterator (probably by overloading copy constructor
of const_iterator class to accept iterator class objects) so that
iterator objects can be assigned to const_iterator objects.
Or I could use inheritance to derive iterator from const_iterator.

I hope my understanding so far is correct up to now.

Can I achieve the same effect by doing something like below rather
than having two different classes which are almost identical?

template <typename T>
class Container {
public:
class ContainerIterator;
typedef ContainerIterator iterator;
typedef const ContainerIterator const_iterator;
// other member functions

iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
private:
// private members
};

template <tyename T>
class Container<T>::ContainerIterator {
friend class Container<T>;
public:
ContainerIterator();
T & operator*();
const T & operator* () const; // so that const_iterator can be
deferenced
iterator & operator++();
const_iterator & operator++() const; // so that const_iterator can
be moved
iterator operator++(int);
const_iterator operator++(int) const;
iterator & operator--();
const_iterator & operator--() const;
iterator operator--(int);
const_iterator operator--(int) const;
bool operator==(const ListIterator &) const;
bool operator!=(const ListIterator &) const;
private:
// private members
};

I would appreciate any comment on whether this would be a viable
alternative or if not, what kind problems this might create. Thank you
in advance.
Jul 22 '05 #1
3 2766

"CoolPint" <co******@yahoo.co.uk> wrote in message
news:15*************************@posting.google.co m...
I would appreciate any comment on whether this would be a viable
alternative or if not, what kind problems this might create. Thank you
in advance.


If I understand you correctly, you want to use a common iterator class
for both const_iterator and iterator and use the constness of that iterator
to determine whether the iterator points to a const object.

This won't work very well, because all someone has to do is copy the
iterator
and it will lose track of whether it needs to be const or not.

const Container cont;
Container::iterator it = cont.begin(); // Now have an non-const iterator
to const object

Jul 22 '05 #2
co******@yahoo.co.uk (CoolPint) wrote in message news:<15*************************@posting.google.c om>...
If I were to design my own container and its iterators, I understand
that I need to provide both "iterator" and "const_iterator" so that
begin() and end() return appropriate ones depending on the
"constantness" of the container object. I also note that is it done
through overloading begin() and end() like below:

iterator begin();
const_iterator begin() const;

So it seems to me that I need to have two separate iterator classes
which are very similar except that const_iterator's operator*()
returns const reference. I also need to provide conversion from
iterator to const_iterator (probably by overloading copy constructor
of const_iterator class to accept iterator class objects) so that
iterator objects can be assigned to const_iterator objects.
Or I could use inheritance to derive iterator from const_iterator.

I hope my understanding so far is correct up to now.

Can I achieve the same effect by doing something like below rather
than having two different classes which are almost identical?

template <typename T>
class Container {
public:
class ContainerIterator;
typedef ContainerIterator iterator;
typedef const ContainerIterator const_iterator;
// other member functions

iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
private:
// private members
};

template <tyename T>
class Container<T>::ContainerIterator {
friend class Container<T>;
public:
ContainerIterator();
T & operator*();
const T & operator* () const; // so that const_iterator can be
deferenced
iterator & operator++();
const_iterator & operator++() const; // so that const_iterator can
be moved
iterator operator++(int);
const_iterator operator++(int) const;
iterator & operator--();
const_iterator & operator--() const;
iterator operator--(int);
const_iterator operator--(int) const;
bool operator==(const ListIterator &) const;
bool operator!=(const ListIterator &) const;
private:
// private members
};

I would appreciate any comment on whether this would be a viable
alternative or if not, what kind problems this might create. Thank you
in advance.


You are mixing semantics of const: a const_iterator is meant to refer
to something const, it is not a const object itself. How can you
implement

const_iterator & operator++() const;

without violating the const modifier on the function? Advancing the
iterator surely does change the iterator object.

Olaf
Jul 22 '05 #3
> If I understand you correctly, you want to use a common iterator class
for both const_iterator and iterator and use the constness of that iterator
to determine whether the iterator points to a const object.
Spot on! That's what I was hoping to achieve and now I understand it
won't work as you point out.
This won't work very well, because all someone has to do is copy the
iterator
and it will lose track of whether it needs to be const or not.

const Container cont;
Container::iterator it = cont.begin(); // Now have an non-const iterator
to const object


Thanks for pointing this out! I also learned that assigning iterator
objects to const_iterator objects are impossible since const_iterator
is
const IteratorClass!
And then I would not be able to have const const_iterator either,
wouldn't I?

Silly me!

So is having separate classes for iterator and const_iterator only
solution in this case?
Jul 22 '05 #4

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

Similar topics

5
by: john smith | last post by:
Hi, I'm a little confused as to why the following code generates and error when compiling: #include <iostream> #include <vector> #include <iterator> using namespace std; void...
2
by: PengYu.UT | last post by:
I'm wonder whether 1. stl directly defined the 6 comparison operators(== != < > <= >=) directly for iterator and const_iterator 2. or it only define == and < and using std::rel_ops to get the...
5
by: John Harrison | last post by:
I there a reliable and generic method to convert a const_iterator to an iterator (i.e. something like const_cast). I ask because I'm writing some methods which take and return iterators. A const...
3
by: Adrian | last post by:
I have some class to read and store options from a config file. I have a design which works for the basic types. What I want to add is an array like type. I am not sure how to define the Field...
4
by: kotau | last post by:
Hi, I'm having trouble with something that would appear to have a simple solution. Here's a version of the code I'm working with: const Item* p 0; name::const_iterator i;
16
by: Juha Nieminen | last post by:
I'm actually not sure about this one: Does the standard guarantee that if there's at least one element in the data container, then "--container.end()" will work and give an iterator to the last...
6
by: Bartholomew Simpson | last post by:
I'm trying to avoid (or at least, minimize) duplicity of effort. I have the following function: void Permissions::Exists(const unsigned int id, std::vector<Permission>::const_iterator& iter) {...
4
by: rakesh.usenet | last post by:
For a particular application of mine - I need a simulation of byte array output stream. * write data onto a stream * getback the contiguous content as an array later for network transport. ...
3
by: Adrian | last post by:
In the following code example I am trying to create a generic interface for a bunch of objects. Each concrete type is stored in its own container and the a container of pointer's to base is used so I...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.