Dynamic Arrays & Class Properties

Denis Koroskin 2korden at gmail.com
Wed Aug 27 08:51:55 PDT 2008


On Wed, 27 Aug 2008 19:47:26 +0400, Mason Green <mason.green at gmail.com>  
wrote:

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

No, there shouldn't be any. But the code doesn't look good.

> 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