constness for arrays
Andrew Fedoniouk
news at terrainformatica.com
Sat Jul 22 01:03:33 PDT 2006
"Derek Parnell" <derek at psych.ward> wrote in message
news:op.tc2lghnq6b8z09 at ginger.vic.bigpond.net.au...
[skiped]
>> method is not an option at all.
>> operator= is a guard of memory loacation and method, well, is method.
>>
>> struct guard {
>> int v;
>> void opAssign(int nv) { alarm("value 'v' is about to change"); v =
>> v; }
>> }
>>
>> guard gv;
>> gv = 12;
>>
>> As you may see operator= guards memory location allowing you to intercept
>> all assignments into the variable. Too many things (RAII, smart pointers)
>> were built
>> around this in C++.
>>
>> Method of the struct will not help you here in principle.
>
> struct guard {
> private int _v;
> void v(int nv) { alarm("value 'v' is about to change"); _v = v; }
> }
> guard gv;
> gv.v = 12 ;
Consider this:
guard gv, gv1;
gv1.v = 24;
gv.v = 12 ;
gv = gv1; //oops, where is my alarm()?
Again there is no method in "modern D" to catch assignment
to the variable.
In my case (wrapper of htmlayout), in following assignment:
dom::element root = dom::element::get_root(hWnd);
operator= calls HTMLayout_useElement of the HELEMENT
returned by get_root. (C++)
And C++ will call destructor for the root at the end of the block.
And in destructor happens HTMLayout_unuseElement.
Such use case allows to hold resources for limited (deterministic)
time. At the end system is more responsive than any GCable one.
There is no way in D to implement this. Sorry, but this is true.
Andrew Fedoniouk.
http://terrainformatica.com
More information about the Digitalmars-d
mailing list