Boost logo

Boost :

From: Thomas Witt (witt_at_[hidden])
Date: 2002-09-12 05:51:46


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Peter,

On Thursday 12 September 2002 12:23, Peter Simons wrote:
> I thought of defining "const_my_custom_iterator" with only
> const member functions, and then to derive the non-const
> "my_custom_iterator" class from that, adding the non-const
> member functions.

Deriving from an iterator is almost never a good idea. the reason is that the
derived iterators operators are likely to have the wrong return type.

Pseudo code:

class A {};
class B {};

class iter
{
...

A operator*();

iter& opertator++();
};

class derived_iter : public iter
{
..
B operator*();

};

int main()
{
derived_iter it;

B b1(*it) ; // Compiles
B b2(*++it); // Fails cannot convert from A to B
}

With regrad to constness, the name prefix const_ tells you nothing about the
constness of the iterator object. The prefix const_ is usually(read in the
stdlib) used if the iterator points to a const object. Thus const_iterator
models A const* whereas a const (no underline here) iterator models A *const.

> Or is there an entirely different, better way, maybe using
> the iterator adaptors?

iterator_adaptors is certainly the way to go.

HTH

- --Thomas

- --
Dipl.-Ing. Thomas Witt
Institut fuer Verkehrswesen, Eisenbahnbau und -betrieb, Universitaet Hannover
voice: +49(0) 511 762 - 4273, fax: +49(0) 511 762-3001
http://www.ive.uni-hannover.de
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE9gHHG0ds/gS3XsBoRAms/AJ9wwyaPFjyQ3huiiJdZTiKam6V7ywCcDmiw
qvLYBt/sp0oy6kTITiHntiU=
=wU+j
-----END PGP SIGNATURE-----


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk