www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.ide
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript
electronics



digitalmars.D.learn - opAssign: cannot set reference of object to null

↑ ↓ ← Mahe <maheweb web.de> writes:
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 );
}
Apr 21 2008
→ Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Mahe wrote:
 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.

Looks like a bug. Please report it here: http://d.puremagic.com/issues/ A workaround is probably to do "b = cast(B) null".
Apr 21 2008
→ "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"Mahe" wrote
 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 );
 }

That is interesting... I know that you cannot overload opAssign with something that implicitly casts to the lvalue type, but what does the compiler do in the case of null? It casts both to the lvalue type, and possibly the argument that is passed to opAssign. IMO, it should not call opAssign, but set the implicit reference. I'd file it as a bug. You may be able to workaround by doing: b = (B)null; But that seems like a lot of extra work. -Steve
Apr 21 2008