Dynamic Arrays & Class Properties
Jarrett Billingsley
kb3ctd2 at yahoo.com
Wed Aug 27 22:24:49 PDT 2008
"Mason Green" <mason.green at gmail.com> wrote in message
news:g93sue$k3v$1 at digitalmars.com...
> 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!
There's no memory/register overhead; ConstArrayReference!(T).sizeof ==
(T[]).sizeof.
And if you use -inline, there's no function call overhead either. :)
More information about the Digitalmars-d-learn
mailing list