Dynamic Arrays & Class Properties

Mason Green mason.green at gmail.com
Wed Aug 27 08:47:26 PDT 2008


Denis Koroskin Wrote:

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

Thanks for the smart solution!  

Unfortunately this seems like a lot of extra overhead... I may just end up keeping the dynamic arrays public!

In this particular case it would be nice to have the option of declaring friend classes, like in C++!!!

Regards,
Mason



More information about the Digitalmars-d-learn mailing list