Use classes as keys in associative array

JN 666total at wp.pl
Sat Jun 6 16:49:29 UTC 2020


Is it possible to use different class instances as keys for 
associative array, but compare them by the contents? I tried to 
override opEquals but it doesn't seem to work. Basically I'd like 
this code to work. I know structs would work but I can't use 
structs for this):

import std.stdio;

class Name
{
     string s;

     this(string s)
     {
         this.s = s;
     }
}

void main()
{
     Name n1 = new Name("John");
     Name n2 = new Name("John");

     int[Name] ages;
     ages[n1] = 50;

     assert(ages[n2] == 50);
}


More information about the Digitalmars-d-learn mailing list