STL inherit from container<T>::iterator

Discussion in 'C++' started by Christoph Heindl, Apr 3, 2006.

  1. Hi,

    I'm currently struggling with a design decision. One of my classes
    (named Attributable) has a protected member of type

    std::list<StrongReference<Attribute> >

    Basically the list has strong references that point to Attribute's.
    In Attributable i want to provide an stl like interface for
    bidirectional iterators like:

    Iterator Attributable::attributes_begin();
    Iterator Atrributable::attributes_end();

    I could easily typedef Iterator as std::list<StrongReference<Attribute>
    >::iterator. However,

    I certainly don't want to have the user of Attributable to access the
    strong reference instance.
    I rather want to hide the fact that there are strong references
    maintained and only provide him access to the actual Attribute pointer.

    To accomplish this, the only way that comes up into my mind is to
    inherit from
    std::list<T>::iterator and override the operators * and ->. However
    since i don't have knowledge about internals of the
    std::list<T>::iterator, I doubt that this is a good idea.

    Any suggestions, links etc.?
    Thanks in advance,
    Christoph
    Christoph Heindl, Apr 3, 2006
    #1
    1. Advertising

  2. Christoph  Heindl

    Daniel T. Guest

    In article <>,
    "Christoph Heindl" <> wrote:

    > Hi,
    >
    > I'm currently struggling with a design decision. One of my classes
    > (named Attributable) has a protected member of type
    >
    > std::list<StrongReference<Attribute> >
    >
    > Basically the list has strong references that point to Attribute's.
    > In Attributable i want to provide an stl like interface for
    > bidirectional iterators like:
    >
    > Iterator Attributable::attributes_begin();
    > Iterator Atrributable::attributes_end();
    >
    > I could easily typedef Iterator as std::list<StrongReference<Attribute>
    > >::iterator. However,

    > I certainly don't want to have the user of Attributable to access the
    > strong reference instance.
    > I rather want to hide the fact that there are strong references
    > maintained and only provide him access to the actual Attribute pointer.
    >
    > To accomplish this, the only way that comes up into my mind is to
    > inherit from
    > std::list<T>::iterator and override the operators * and ->. However
    > since i don't have knowledge about internals of the
    > std::list<T>::iterator, I doubt that this is a good idea.
    >
    > Any suggestions, links etc.?


    Inheriting from a standard iterator is a bad idea. It is likely that any
    code you put in the class would be just as useful for any of the other
    standard iterators (other containers could hold StrongReferences as
    well. Best would be to write a class that contains the list::iterator
    and have your class create one of them with the iterator inside it.


    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
    Daniel T., Apr 3, 2006
    #2
    1. Advertising

  3. hi Daniel,

    thanks for hint. Seems like a very reasonable idea. I gonna give it a
    try!

    best regards,
    christoph
    Christoph Heindl, Apr 4, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vivi Orunitia
    Replies:
    11
    Views:
    4,451
    Martijn Lievaart
    Feb 4, 2004
  2. forester
    Replies:
    16
    Views:
    709
    Howard Hinnant
    Aug 25, 2005
  3. Replies:
    4
    Views:
    786
    Daniel T.
    Feb 16, 2006
  4. T.A.
    Replies:
    21
    Views:
    864
    Pete Becker
    Sep 25, 2006
  5. gallows
    Replies:
    3
    Views:
    798
    gallows
    Mar 5, 2007
Loading...

Share This Page