Dynamic Arrays & Class Properties

Denis Koroskin 2korden at gmail.com
Wed Aug 27 07:15:34 PDT 2008


On Wed, 27 Aug 2008 17:59:09 +0400, Mason Green <mason.green at gmail.com>  
wrote:

> Hi,
>
> Anyone know how to expose dynamic array elements via class properties?   
> I would like to do something this this:
>
> class Foo {
>
>     private int[] m_dummy;
>
>     this() {
>         m_dummy ~= 19;
>         m_dummy ~= 77;
>     }
>
>     int dummy( ??????? ) {
>         return m_dummy[x];
>     }
> }
>
> void main() {
>     auto foo = new Foo();
>     Cout(foo.dummty[0]);     // Print 19
> }
>
> Any help would be much appreciated!  The question marks are where I'm  
> stuck....
>
> Thanks,
> Mason
>

maybe something like this:

struct ConstArrayReference(T)
{
         T opIndex(int index) {
                 return array[index];
         }

         private T[] array;
}

class Foo
{
     private int[] m_dummy;

     this() {
         m_dummy ~= 19;
         m_dummy ~= 77;
     }

     ConstArrayReference!(int) dummy() {
         ConstArrayReference!(int) r = { m_dummy };
         return r;
     }
}

void main() {
     auto foo = new Foo();
     Cout(foo.dummy[0]).newline;     // Prints 19
}


Too bad we don't have references (yet?) :(


More information about the Digitalmars-d-learn mailing list