Private container, public iterator

Q

quantdev2004

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
 
P

Pete Becker

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top