opAssign: cannot set reference of object to null

Mahe maheweb at web.de
Mon Apr 21 11:36:04 PDT 2008


Hi, 
in the following code the statement "b = null" calls "B opAssign( A v )" and causes an access violation. I don't understand why! How can I set b to null?
I'm using dmd 1.028.

class A
{
	this( int i )
	{
		a = i;
	}
	int a;
}

class B
{
	this( int i )
	{
		a = i;
	}
	int a;
	
	B opAssign( A v )
	{
		a = v.a;
		return this;
	}
}

int main(char[][] args)
{

	A a = new A( 4 );
	assert( a.a == 4 );
	B b ;
	assert( b is null );
	b = new B( 5 );
	b = a;
	assert( b.a == 4 );
	b = null;
	assert( b is null );
}


More information about the Digitalmars-d-learn mailing list