J
John
Hi,
I have the following classes
class A {};
class B
{
std::map<int,A*> mmap;
public:
std::map<int,A*>::const_iterator begin() const
{ return mmap.begin(); }
std::map<int,A*>::const_iterator end() const
{ return mmap.end(); }
}
Since end/begin return const_iterators, I know that the keys of the map
cannot be changed. Also, the pointers in the values of the map cannot
be changed. However, the A objects being pointed to can be altered.
What I would like is to be able to return an iterator to the map and
protect not only the map itself but keep for the pointer to be constant
as well, i.e., const A*. Or in other words, have behavior like
std::map<int,const A*>::const_iterator begin() const
{ return mmap.begin(); }
even though mmap internally is actually std::map<int,A*> .
Is there any way to do this (preferably without using const_cast). I
have tried a number of things, but can't seem to get it right.
Thanks,
John
I have the following classes
class A {};
class B
{
std::map<int,A*> mmap;
public:
std::map<int,A*>::const_iterator begin() const
{ return mmap.begin(); }
std::map<int,A*>::const_iterator end() const
{ return mmap.end(); }
}
Since end/begin return const_iterators, I know that the keys of the map
cannot be changed. Also, the pointers in the values of the map cannot
be changed. However, the A objects being pointed to can be altered.
What I would like is to be able to return an iterator to the map and
protect not only the map itself but keep for the pointer to be constant
as well, i.e., const A*. Or in other words, have behavior like
std::map<int,const A*>::const_iterator begin() const
{ return mmap.begin(); }
even though mmap internally is actually std::map<int,A*> .
Is there any way to do this (preferably without using const_cast). I
have tried a number of things, but can't seem to get it right.
Thanks,
John