Boost logo

Geometry :

Subject: [ggl] space partitioning
From: Barend Gehrels (barend.gehrels)
Date: 2010-09-10 07:15:45

  hi Adam,

> Is it undefined even if the object of derived class was created in the
> first place and the pointer casted to the base type? Isn't it used in
> Boost.Intrusive?
> struct node {}
> struct internal_node : public node {}
> struct leaf_node : public node {}
> internal_node *in = new internal_node();
> node *n = static_cast<node*>(in);
> /* do something with node n */

This downcast does not need a cast at all.
node* n = in;
will do

> internal_node *in2 = static_cast<internal_node*>(n);
> /* do something with internal node n2 */

I would select a dynamic_cast here, which gives you a internal_node or
NULL if it is not a internal_node. This needs RTTI. For gcc, this needs
at least one virtual method (e.g. destructor).
Because Boost.Geometry hardly uses dynamic memory itself, it is all done
by std::vector etc, I didn't dive into how this is done in Boost in
general and if they encourage it (RTTI).

> I have objections about following code where object of derived type 1
> is created, and the pointer is casted to the base class and then to
> the derived type 2 extending the interface of the base class.

Me too, this sounds like a bug. However, if you use dynamic casts and
RTTI, you can check whether it IS a type 2 object, and only if it is
one, do something with it.

For spatial index, it might seem necessary to work like this, the
approach of Federico also contained nodes and leaves, dynamically
allocated. I'm not sure if it can be avoided here. But he didn't need
the casts, because there are two distinct objects:
     typedef boost::shared_ptr<rtree_node<Box, Value> > node_pointer;
     typedef boost::shared_ptr<rtree_leaf<Box, Value> > leaf_pointer;

Thanks for your attached code, I don't know if I've the time today to
dive into it, otherwise it will be at least a week later.

Regards, Barend

Geometry list run by mateusz at