Topic: merits of using iterators versus pointer returned by op->


Author: jkanze@otelo.ibmmail.com
Date: 1998/06/26
Raw View
In article <u0ra0exh9o.fsf@heat.co.symbios.com>,
  Carley Williams <carley@heat.co.symbios.com> wrote:
>
>
> I'm trying to understand the relative merits of using the the -> and *
> container::iterator operators verses using the pointer or reference
> returned by these operators.  More specifically in the following code
> fragment, what are the relative dangers of *b, b-> versus *p, p->
>
>   class bar;
>   bar x;
>   list<bar> foo;
>   foo.push_back(x);
>   list<bar>::iterator b = foo.begin();
>   // get pointer to object inside the container
>   bar* p = b.operator->();
>   // use pointer
>   cout << "p = " << *p << endl;
>
> I currently "feel" that use of *b, b-> is "safer" but can not really
> explain or justify via the standard why I feel this way.

It depends on the implementation of the iterator.  The iterator can
be made safer -- at least one implementation does (or did).  For example,
if the object designated by the iterator is removed from the list, the
iterator can cause an assertion failure when you try and access it; while
an implementation could presumably cause the same thing to happen with
a pointer (Purify more or less does), it's much more difficult, more
expensive in run-time, and less likely to happen.

--
James Kanze    +33 (0)1 39 23 84 71    mailto: kanze@gabi-soft.fr
        +49 (0)69 66 45 33 10    mailto: jkanze@otelo.ibmmail.com
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique orientie objet --
              -- Beratung in objektorientierter Datenverarbeitung

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Carley Williams <carley@heat.co.symbios.com>
Date: 1998/06/25
Raw View

I'm trying to understand the relative merits of using the the -> and *
container::iterator operators verses using the pointer or reference
returned by these operators.  More specifically in the following code
fragment, what are the relative dangers of *b, b-> versus *p, p->

  class bar;
  bar x;
  list<bar> foo;
  foo.push_back(x);
  list<bar>::iterator b = foo.begin();
  // get pointer to object inside the container
  bar* p = b.operator->();
  // use pointer
  cout << "p = " << *p << endl;

I currently "feel" that use of *b, b-> is "safer" but can not really
explain or justify via the standard why I feel this way.

Can someone straighten me out or provide justification if iterators are
safer.

Some context may help. I'm trying to implement the interface to an old
container class library using stl containers.  Many of the methods in
the old library have a pointer reference parameter (e.g. int next(t*& p)
).  "p" is assigned a pointer to an object inside the container.

one (non-templatized) solution is:

class myBarList
{
  private:
     std::list<bar> imp;
     std::list<bar>::iterator impiter;
  public:
     myBarList() { impiter = imp.begin() }
     int next(bar*& p)
     {
       if (impiter == imp.end()) return 0;
       ++impiter ;
       if (impiter == imp.end()) return 0;
       p = impiter.operator->();
       return 1;
     }
}

Are there dangers to letting the user have this pointer?

Thanks.

--carley

--------------------------------------------
Carley D. Williams
Symbios Inc.
3351 Eastbrook Drive
Fort Collins, CO 80525
FAX:   (970) 225-4829
PHONE: (970) 223-5100 x9421
EMAIL: Carley.Williams@symbios.com
--------------------------------------------

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]