Ah hello.
I have been very kindly helped out on a previous question by EvilRix which resulted in me getting the following custom iterator class for a std::map.
class CCustomMapIterator
{
public:
typedef std::map<CString, CInfo*> storage_type;
CCustomMapIterator(storage
_type::ite
rator itr): m_itr(itr)
{}
bool operator == (const CCustomMapIterator& lhs ) const { return lhs.m_itr == m_itr; }
bool operator != ( const CCustomMapIterator& lhs ) const { return ! ( *this == lhs ); }
CCustomMapIterator operator=(storage_type::it
erator itr) { m_itr = itr; }
bool operator == (const storage_type::iterator& iter ){ return iter == m_itr; }
bool operator != (const storage_type::iterator& iter ){ return ! ( iter == m_itr ); }
CInfo* operator *() { return m_itr->second; }
storage_type::const_iterat
or operator ++() { return ++m_itr; }
storage_type::const_iterat
or operator --() { return --m_itr; }
private:
storage_type::const_iterat
or m_itr;
};
Basically, this class allows us to always return the second element in a map. So we can say
std::map<CString, CInfo*> g_map;
CCustomMapIterator iter = g_map.begin();
CInfo* pInfo = *iter;
without the need to use the ->second notation.
(Not much benefit *it may seem*, but if you have the inclination, read my question at
http:Q_23080582.html for a full benefits discussion.)
Anyway. I soon realised that I have a lot of std::maps in my code, and would like to use this principle for all of them. To save defining a new class, I thought I would try and templatize the above.
That is where the problems came.
Consider this:
template <class T, class U> class CCustomMapIterator
{
public:
typedef std::map<class T, class U*> storage_type;
CCustomMapIterator(storage
_type::ite
rator itr): m_itr(itr)
{}
CCustomMapIterator operator=(storage_type::it
erator itr) { m_itr = itr; }
bool operator == (const CCustomMapIterator& lhs ) const { return lhs.m_itr == m_itr; }
bool operator != ( const CCustomMapIterator& lhs ) const { return ! ( *this == lhs ); }
bool operator == (const storage_type::iterator& iter ){ return iter == m_itr; }
bool operator != (const storage_type::iterator& iter ){ return ! ( iter == m_itr ); }
U* operator *() { return m_itr->second; }
storage_type::const_iterat
or operator ++() { return ++m_itr; }
storage_type::const_iterat
or operator --() { return --m_itr; }
private:
storage_type::const_iterat
or m_itr;
};
I thought that was ok, so I tried it with similar code to before:
std::map<CString, CInfo*> g_map;
CCustomMapIterator<CString
, CInfo> iter1= g_map.begin();
The last line fails with
// ERROR: cannot convert from 'std::_Tree<_Traits>::iter
ator' to 'CCustomMapIterator<T,U>'
Is there a flaw in what I am trying to do; if so, what ?
TIA