Best way to reference an array in a child class...

Steven Schveighoffer schveiguy at yahoo.com
Thu Mar 6 12:02:14 PST 2014


On Thu, 06 Mar 2014 14:47:49 -0500, Ali Çehreli <acehreli at yahoo.com> wrote:

> On 03/06/2014 11:40 AM, Steven Schveighoffer wrote:
>
>  >> class A
>  >> {
>  >>    S[]* pointer_to_list;
>  >>    abstract...
>  >> }
>
>  > I would highly suggest to just use S[] and not S[]*. A slice is  
> already
>  > a reference (coupled with a length).
>
> But what if there are elements added to B.items later on? I assumed the  
> OP wanted a true reference.

First, it's very cumbersome to work with a pointer to an array. All array  
syntax sugar does not peek through the reference, you have to * everything.

Second, it's very difficult to get an array, just by itself, in the heap.  
And you don't want to store a reference to a stack-frame slice.

I think there is a good reason you seldom see code that has pointers to  
array in D, the above code snippet seems to me like a programmer who  
doesn't understand what a D slice is.

Actually, given the subject of this post, I take it back. The best way to  
reference an array in a child class, especially one of a static type, is  
to not have another copy in the child class :)

>  > You can read more about D arrays and slices here:
>  >
>  > http://dlang.org/d-array-article.html
>
> My I suggest you do the same. :o)

TL;DR ;)

-Steve


More information about the Digitalmars-d-learn mailing list