user1277936
user1277936

Reputation:

Simple operator overload in D language throws exception

This code:

class C 
{
    int opAdd(C b) { return 1; }
    private int j;
}

void main() 
{
    C a;
    C c;

    int j = a + c;
}

Throws:

"object.Exception: Access Violate - Read at address 0x0"

Upvotes: 0

Views: 200

Answers (1)

Vladimir Panteleev
Vladimir Panteleev

Reputation: 25187

Classes in D are reference types. You need to instantiate them:

C a = new C();
C b = new C();

Also, opAdd has been replaced by opBinary!"+" in D2 (see D2 operator overloading).

Upvotes: 6

Related Questions