opAssign: cannot set reference of object to null
Steven Schveighoffer
schveiguy at yahoo.com
Mon Apr 21 12:03:06 PDT 2008
"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
More information about the Digitalmars-d-learn
mailing list