Confused about class equality

Jacob Carlborg doob at me.com
Sat Apr 3 03:22:34 PDT 2010


On 4/3/10 07:03, strtr wrote:
> What I probably mean to ask is :
> In the code below, for what kind of i1 and i2 would the output be like this :
> ---------
> Same Value.
> 3 : 5B536C 59D020
> 3 : 59CE0C 59CEF0
> 5 : 5B536C 59D020
> 5 : 59CE0C 59CEF0
> ---------
> if( i1 !is null&&  i2 !is null&&  i2.value == i1.value ) {
> 	writefln("Same Value.");
> 	if( i2 is i1 ) writefln("Same Object.");
> 	writefln( i2.toString()," : ",i2.__vptr," ",i2.__monitor);
> 	writefln( i1.toString(), " : ", i1.__vptr," ",i1.__monitor);
> 	i1.value = 5;
> 	writefln( i2.toString()," : ",i2.__vptr," ",i2.__monitor);
> 	writefln( i1.toString(), " : ", i1.__vptr," ",i1.__monitor);
> }
>
>

I would guess:

i1 = new I;
i2 = new I;

They are two different instance of "I" but containing the same value. 
When comparing two objects using "is", the addresses are compared and 
not the data in the object, i.e. instance variables.


More information about the Digitalmars-d-learn mailing list