Private container, public iterator

Discussion in 'C++' started by quantdev2004@yahoo.co.uk, Mar 17, 2005.

  1. Guest

    Dear NG,

    I have seen this code and was wondering about its meaning:

    class MyClass
    {

    private:
    typedef stl::hash_map<stl::string,
    int,
    FHashTraitsStringNoCase> string_2_int;

    string_2_int m_String2Int;
    public:

    typedef string_2_int::const_iterator string_2_int_const_iterator;

    // Iterate over the string_2_int
    string_2_int_const_iterator BeginStringInt() const
    {
    return m_String2Int.begin();
    }
    }


    The meaning of this puzzles me. What is this achieving? Hiding the
    string_2_int type, but declaring an iterator to that same type to be
    public, does not look like it's encapsulating anything.

    Can someone please expand a bit on what is exactly happening and why
    would one want to choose this approach?

    Thanks,
    QD
    , Mar 17, 2005
    #1
    1. Advertising

  2. Pete Becker Guest

    wrote:

    >
    > The meaning of this puzzles me. What is this achieving? Hiding the
    > string_2_int type, but declaring an iterator to that same type to be
    > public, does not look like it's encapsulating anything.


    Especially since the exposed name of the type is
    string_2_int_const_iterator. The convention throughout the STL is that
    containers have typedefs named 'iterator, 'const_iterator', etc.

    >
    > Can someone please expand a bit on what is exactly happening and why
    > would one want to choose this approach?
    >


    Aside from the naming issues, what it does is provide a sequence
    delineated by a pair of iterators. That sequence can be operated on by
    STL algorithms. This is done without exposing the details of how the
    sequence is stored.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, Mar 17, 2005
    #2
    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. Charles A. Lackman
    Replies:
    1
    Views:
    1,324
    smith
    Dec 8, 2004
  2. SpamProof
    Replies:
    0
    Views:
    531
    SpamProof
    Oct 21, 2003
  3. qazmlp
    Replies:
    19
    Views:
    776
    Daniel T.
    Feb 4, 2004
  4. DaveLessnau
    Replies:
    3
    Views:
    413
    Howard
    May 16, 2005
  5. Goran
    Replies:
    3
    Views:
    289
Loading...

Share This Page