OSNews article about C++09 degenerates into C++ vs. D discussion

Steve Horne stephenwantshornenospam100 at aol.com
Fri Nov 24 10:25:00 PST 2006


On Fri, 24 Nov 2006 10:19:33 +0100, Frits van Bommel
<fvbommel at REMwOVExCAPSs.nl> wrote:

>Steve Horne wrote:
>> BUT - how do cyclic references occur in relation to slicing? It sounds
>> very odd. I can certainly see the point of chains (slices of slices)
>> but I can't see how cycles could arise at all.
>
>Take this type:
>
>struct S {
>	S[] slice;
>	// ... some more stuff
>}
>
>Then just allocate some arrays of these and set members of their 
>elements to slices of them.

OK, that certainly answers my question.

OTOH why would you do that? This doesn't look to me like something
that would arise in practice.

If you have an object of type S that contains (a reference to) an
array of type S, that suggests some kind of digraph data structure -
eg a tree - to me. Recursive types are almost always data structure
related. And in this case, it's a type that probably needs rethinking
- it is generally much better for each node to have a (fixed size)
array of references to other nodes rather than a single reference to
an array of other nodes.

Anyway, if this is a data structure node, you really don't want to
assign a slice to that array. If you remove items by slicing (using a
reference-based slicing method) you never really remove dead items.
You only hide them, since the original array remains so that the slice
can reference it. To do genuine removal you need to either modify the
original array, or else copy from the slice to create a replacement
array.

-- 
Remove 'wants' and 'nospam' from e-mail.



More information about the Digitalmars-d mailing list