Re: maps, iterators, and const
* John, on 21.05.2010 17:54:
Solve this by creating your own iterators which inherit privately from
the map's iterators and don't expose the map's iterators in your
custom interface.
/Leigh
Hmm. This would be more complicated than I would like since I don't want
to have to define additional classes. It's not ideal, but maybe I will
just go the const_cast route and define my map as std::map<int,const A*>
and use const_cast internally in the few places I need to alter an A.
Possibly you have design level problem, but possibly you have not.
You might consider something like the following:
<code>
#include <map>
#include <iostream>
class A {};
class B
{
typedef std::map< int, A* > Map;
Map map_;
B( B const& other );
B& operator=( B const& other );
public:
B()
{
map_[1] = new A;
map_[2] = new A;
map_[3] = new A;
}
class ConstIter
{
friend class B;
private:
typedef std::pair< int, A const* > ConstPair;
Map::const_iterator current_;
ConstIter( Map::const_iterator start )
: current_( start )
{}
ConstPair const* operator->() const; // No such.
public:
ConstIter& operator++()
{
++current_; return *this;
}
ConstIter operator++( int )
{
return ConstIter( current_++ );
}
ConstPair const operator*() const
{
return *current_;
}
bool operator==( ConstIter const& other )
{
return current_ == other.current_;
}
bool operator!=( ConstIter const& other )
{
return current_ != other.current_;
}
int key() const { return current_->first; }
A const* value() const { return current_->second; }
};
ConstIter begin() const { return map_.begin(); }
ConstIter end() const { return map_.end(); }
};
int main ()
{
B b;
for( B::ConstIter it = b.begin(); it != b.end(); ++it )
{
std::cout << it.key() << std::endl;
}
}
</code>
Cheers & hth.,
- Alf
--
blog at <url: http://alfps.wordpress.com>