iterator_adaptor Question
I have a class that holds a container of pointers to polymorphic Base
classes. I'd like clients who iterate over those Bases using a mutating
iterator type to be able to call non-const Base class member functions (but
I do not want those clients to be able to replace the Base* directly). I'd
also like clients who iterate using a const_iterator type to be able to call
only const Base class member functions.
In the following test code, I've attempted to use boost::iterator_adaptor to
create a const iterator with these properties, but the const_iterator still
allows calls to non-const operations. Can anyone help me see where I've
gone wrong?
#include
iterator;
typedef boost::iterator_adaptor< Bases_imp::const_iterator, boost::default_iterator_policies, Base const*, Base const* const&, Base const* const*
const_iterator;
void foo() const { const_iterator i(bases_.begin()); const_iterator end(bases_.end()); for (; i != end; ++i) (*i)->foo(); // Oops - const_iterator allows call to non-const method } Bases_imp bases_; };
"Hickman, Greg"
I have a class that holds a container of pointers to polymorphic Base classes. I'd like clients who iterate over those Bases using a mutating iterator type to be able to call non-const Base class member functions (but I do not want those clients to be able to replace the Base* directly). I'd also like clients who iterate using a const_iterator type to be able to call only const Base class member functions.
In the following test code, I've attempted to use boost::iterator_adaptor to create a const iterator with these properties, but the const_iterator still allows calls to non-const operations. Can anyone help me see where I've gone wrong?
Umm, right....
#include
#include #include <iostream> #include <list> class Base : private boost::noncopyable { public: virtual ~Base() {} virtual void foo() const = 0; here?------------------^^^^^ };
HTH, Dave
--- In Boost-Users@y..., "David Abrahams"
Umm, right....
#include
#include #include <iostream> #include <list> class Base : private boost::noncopyable { public: virtual ~Base() {} virtual void foo() const = 0; here?------------------^^^^^ };
I'm such a dolt! Thanks for debugging my code. Greg
participants (3)
-
David Abrahams
-
greg_hickman3
-
Hickman, Greg