Jump to page: 1 2
Thread overview
Best way to reference an array in a child class...
Mar 06, 2014
captain_fid
Mar 06, 2014
captain_fid
Mar 06, 2014
Ali Çehreli
Mar 06, 2014
Ali Çehreli
Mar 06, 2014
captain_fid
Mar 06, 2014
captain_fid
Mar 06, 2014
captain_fid
Mar 06, 2014
captain_fid
Mar 07, 2014
bearophile
Mar 07, 2014
captain_fid
Mar 07, 2014
captain_fid
Mar 06, 2014
Ali Çehreli
Mar 06, 2014
captain_fid
Mar 06, 2014
Ali Çehreli
March 06, 2014
Sorry for the very basic question. Much still alludes me with this language. I appreciate the forum.

struct S
{
March 06, 2014
On Thursday, 6 March 2014 at 19:19:29 UTC, captain_fid wrote:
> Sorry for the very basic question. Much still alludes me with this language. I appreciate the forum.
>
> struct S
> {

Wow sorry for that. I'm a moron... don't press <tab> <enter>...

struct S
{
  int a;
  string b;
}

class A
{
  S[]* pointer_to_list;
  abstract...
}

class B: A
{
  S[] items = [ {10, "first"}, {20, "second"}];

  this() {
   pointer_to_list = &items;
  }
}

My problem is in de-referencing later (seg fault). Is this even the best way?

I Really need to access the array in a derived class. 'S' (I believe) really is best as a Structure.

Any suggestions. Thanks in advance (and sorry for the rough start).


March 06, 2014
On Thu, 06 Mar 2014 14:31:52 -0500, captain_fid <bell.hue@gmail.com> wrote:

> On Thursday, 6 March 2014 at 19:19:29 UTC, captain_fid wrote:
>> Sorry for the very basic question. Much still alludes me with this language. I appreciate the forum.
>>
>> struct S
>> {
>
> Wow sorry for that. I'm a moron... don't press <tab> <enter>...
>
> struct S
> {
>    int a;
>    string b;
> }
>
> class A
> {
>    S[]* pointer_to_list;
>    abstract...
> }
>
> class B: A
> {
>    S[] items = [ {10, "first"}, {20, "second"}];
>
>    this() {
>     pointer_to_list = &items;
>    }
> }
>
> My problem is in de-referencing later (seg fault). Is this even the best way?
>
> I Really need to access the array in a derived class. 'S' (I believe) really is best as a Structure.
>
> Any suggestions. Thanks in advance (and sorry for the rough start).

I would highly suggest to just use S[] and not S[]*. A slice is already a reference (coupled with a length).

You can read more about D arrays and slices here:

http://dlang.org/d-array-article.html

-Steve
March 06, 2014
On 03/06/2014 11:31 AM, captain_fid wrote:
> On Thursday, 6 March 2014 at 19:19:29 UTC, captain_fid wrote:
>> Sorry for the very basic question. Much still alludes me with this
>> language. I appreciate the forum.
>>
>> struct S
>> {
>
> Wow sorry for that. I'm a moron... don't press <tab> <enter>...
>
> struct S
> {
>    int a;
>    string b;
> }
>
> class A
> {
>    S[]* pointer_to_list;
>    abstract...
> }
>
> class B: A
> {
>    S[] items = [ {10, "first"}, {20, "second"}];
>
>    this() {
>     pointer_to_list = &items;
>    }
> }
>
> My problem is in de-referencing later (seg fault). Is this even the best
> way?
>
> I Really need to access the array in a derived class. 'S' (I believe)
> really is best as a Structure.
>
> Any suggestions. Thanks in advance (and sorry for the rough start).
>
>

You need to dereference the pointer by * and you would be using a slice:

import std.stdio;
import std.conv;

struct S
{
    int a;
    string b;
}

class A
{
    S[]* pointer_to_list;

    void access()
    {
        foreach (e; *pointer_to_list) {    // <-- NOTE *
            writeln(e);
        }
    }
}

class B: A
{
    S[] items = [ {10, "first"}, {20, "second"}];

    this() {
        pointer_to_list = &items;
    }
}

void main()
{
    auto b = new B();

    foreach (i; 30 .. 40) {
        b.items ~= S(i, i.to!string);
    }

    b.access();
}

As a side note, I would make A take the slice as a reference parameter, rather that B accessing A's member directly. It is better for maintainability:

class A
{
    S[]* pointer_to_list;

    this(ref S[] list)    // <-- BETTER
    {
        this.pointer_to_list = &list;
    }

// ...
}

class B: A
{
// ...

    this() {
        super(items);    // <-- BETTER
    }
}

Ali

March 06, 2014
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.

> 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)

> -Steve

Ali

March 06, 2014
On Thu, 06 Mar 2014 14:47:49 -0500, Ali Çehreli <acehreli@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
March 06, 2014
On Thu, 06 Mar 2014 15:02:14 -0500, Steven Schveighoffer <schveiguy@yahoo.com> wrote:

> 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.

Sorry, I meant an array *reference*, not an array. Clearly allocating an array in the heap is easy, but allocating a pointer to an array is not :)

-Steve
March 06, 2014
Steve and Ali, Thanks for the quick helpful suggestions. In this case I probably don't need the expansion (but certainly look forward to understanding either way).

I'll certainly hit that reference (Special thanks Ali for your book. I've enjoyed it over the past few months)

March 06, 2014
On 03/06/2014 12:02 PM, Steven Schveighoffer wrote:

> 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 :)

Agreed. Alternatively, a member function in the child class could return a slice.

Ali

March 06, 2014
On Thursday, 6 March 2014 at 21:26:11 UTC, Ali Çehreli wrote:
> On 03/06/2014 12:02 PM, Steven Schveighoffer wrote:
>
> > 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 :)
>
> Agreed. Alternatively, a member function in the child class could return a slice.
>
> Ali

Steve, thanks for the link and the nicely written article. Also for the assessment on pointer syntax, I'd love to avoid if possible (especially w/ limitations you noted).

I had been spending time over at http://dlang.org/arrays.html and had forgotten (or never understood) dynamic arrays were passed by slices. That link only talks about passing static arrays (IIRC). Keeping track of 'what being passed how' is tough for this programmer.

Your suggestion Ali (of not accessing the base member in the child was great) and it works properly.

Believe if I understand what you are suggesting above is never to have the array in base? simply retrieve slice through the child member function?
« First   ‹ Prev
1 2