constness for arrays
Andrew Fedoniouk
news at terrainformatica.com
Fri Jul 21 13:27:24 PDT 2006
"Ben Phillips" <Ben_member at pathlink.com> wrote in message
news:e9rc1u$1g71$1 at digitaldaemon.com...
> >
>>operator "=" IS really necessary. As there is no method in D
>>currently to guard assignment to variable (memory location).
>>Again without it good chunk of RAII methods and smart pointers
>>are not implementable in D.
>>
>
> It is impossible to allow operator "=" to be overloaded without totally
> killing
> the way D works because D uses references.
> Example:
> ClassA a = new ClassA();
> ClassA b = a; // b now refers to a
> b.mutate(); // both 'b' and 'a' are changed since they refer to the same
> object
>
I think that operator= shall be available only for structs and probably
other value types.
So it will be no conflict with current situation.
> What is possible is to define a new operator (such as ":=") that means
> copy
> assignment, but I don't see how this differs from creating a method that
> does
> the same thing.
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.
Andrew Fedoniouk.
http://terrainformatica.com
More information about the Digitalmars-d
mailing list