RE: [libxml++] node iterators



> > One problem I faced is the type return by iterator::operator*().
> > I first wanted to return a Node &. It is, I think, more 
> > logical, and avoid 
> > writing things like (*iter)->do_something().
> > However this change the use of dynamic_cast to 'test' the 
> > type of node. If the 
> > cast fail we have an exception instead of just returning a 
> > null pointer.
> > 
> > So the question is :
> > What do you expect as a return type for 
> > Node::ChildrenList::iterator::operator*() ?
> 
> I always use Node& for such code.  (In more detail,
> I use a T& if the value is copied into the container,
> and if modifying the value in the container does not
> affect the value copied from; I use T* if modifying
> the value 
> 
> To get a non-trapping dynamic cast, the user can always
> take the address:
> 
> 	dynamic_cast<Special_Node*>(&(*iter))

Some disadvantages:
- We use Node* in the rest of the API, I think.
- We currently have a NodeList that contains Node*, so people are used to
this:
 
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/libxmlplusplus/libxml++/examp
les/dom_parser/main.cc?rev=HEAD&content-type=text/vnd.viewcvs-markup
- It encourages copy-by-value, which would not work, which would confuse
people.

So, it's not perfect, but I'd prefer
(*iter)->do_something();
and I would actually put it on two lines in the examples, like we already
do.

Murray Cumming
murrayc usa net
www.murrayc.com 




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]