Confused about class equality

Justin Spahr-Summers Justin.SpahrSummers at gmail.com
Fri Apr 2 20:20:36 PDT 2010


On Fri, 02 Apr 2010 22:36:40 -0400, strtr <strtr at spam.com> wrote:
> 
> The program below outputs, as I would expect :
> Same Value.
> Same Object.
> 3 : 44E15C 0000
> 3 : 44E15C 0000
> 5 : 44E15C 0000
> 5 : 44E15C 0000
> 
> Now what would it mean if it were to output :
> Same Value.
> 3 : 5B536C 59D020
> 3 : 59CE0C 59CEF0
> 5 : 5B536C 59D020
> 5 : 59CE0C 59CEF0
> (Output from essentially the same piece of code within a larger project)
> 
> Not the same object, but still able to change it with one call.
> What is it I don't get?
> 
> ------
> module main;
> 
> import std.stdio;
> import std.string;
> 
> interface I{
> 	char[] toString();
> 	int value();
> 	void value(int v);
> }
> 
> class C : I{
> 	private int _value;
> 	
> 	this(int v){ _value = v; }
> 	char[] toString(){ return format(_value); };
> 	int value(){ return _value; };
> 	void value(int v){ _value = v; };
> }
> private I[3][3] arr;
> 
> public I getI( int x, int y){ return arr[x][y]; }
> public void setI( int x, int y, I i ){ arr[x][y]= i; }
> 
> void main(){
> 	C c = new C(3);
> 	setI( 0, 0, c );
> 	setI( 1, 2, c );
> 	I i1 = null;

You may want to try reducing the code within your "larger project" to 
the smallest use case where the problem still occurs, because, I agree, 
the code as written will always result in the same object, since you 
allocate only one.


More information about the Digitalmars-d-learn mailing list