Specifically, I'd like to prevent clients from writing the two lines preceeding the return from main below. Can anyone help?
Eh? You wrote up your sample so anyone could easily compile it... and I did. On VC++ v6, I got two compiler errors: samplecode.cpp(58) : error C2663: 'reset' : 2 overloads have no legal conversion for 'this' pointer samplecode.cpp(59) : error C2678: binary '=' : no operator defined which takes a left-hand operand of type 'const class boost::shared_ptr<struct Shape const >' (or there is no acceptable conversion) These errors are on the two lines you want to prevent. My first impression of the code was that it was already illegal to perform those two actions. You shouldn't be able to call non-const functions through a const_iterator, and since neither reset() nor operator=() are const, no problem. Right? I wasn't expecting the first error message, but the second is in line with my expectations. I take it this code compiles for you? What tools/OS/etc are you using? --Mark Storer Software Engineer Cardiff Software #include <disclaimer> typedef std::disclaimer<Cardiff> Discard; -----Original Message----- From: Hickman, Greg [mailto:greg.hickman@lmco.com] Sent: Friday, November 01, 2002 2:22 PM To: Boost-Users (Boost-Users@yahoogroups.com) Subject: [Boost-Users] Iterator Adaptor Question The following code illustrates a problem I'm having trying to use iterator_adaptor. Basically, I have a container of shared pointers to mutable, and I'd like to provide a const_iterator to clients that disallows modification of both the shared pointer as well as the pointee. Specifically, I'd like to prevent clients from writing the two lines preceeding the return from main below. Can anyone help? Thanks, Greg #include <boost/iterator_adaptors.hpp> #include <boost/shared_ptr.hpp> #include <vector> using namespace boost; using namespace std; struct Shape { virtual ~Shape() {} virtual void draw() const = 0; virtual void update() = 0; }; struct Concrete_shape : Shape { void draw() const {} void update() {} }; typedef shared_ptr<Shape> Shape_ptr; typedef shared_ptr<const Shape> Const_shape_ptr; struct Picture { typedef vector<Shape_ptr> Shapes; // Declare a const_iterator that converts // to the underlying Shape_ptr to a // Const_shape_ptr. Since the returned // Const_shape_ptr is a temporary, clients // shouldn't be allowed to modify it. typedef iterator_adaptor< Shapes::const_iterator, default_iterator_policies, value_type_is<Const_shape_ptr>, reference_is<const Const_shape_ptr>, iterator_category_is<input_iterator_tag>
const_iterator;
const_iterator begin() const { return const_iterator(shapes_.begin()); } Shapes shapes_; }; int main(int argc, char* argv[]) { Picture pic; const Picture& cpic = pic; pic.shapes_.push_back(Shape_ptr(new Concrete_shape)); Picture::const_iterator i = cpic.begin(); // I'd like to prevent clients from writing... i->reset(new Concrete_shape); *i = Const_shape_ptr(new Concrete_shape); return 0; } Info: <http://www.boost.org> Wiki: <http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl> Unsubscribe: <mailto:boost-users-unsubscribe@yahoogroups.com> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
participants (1)
-
Mark Storer