i have project have class state uses templates. have class cell, , use state, state holds cell genericstate. have generic function checks if 2 instances equal. problem is, never leaves state equals method cell equals method.
public class state<t> { public t genericstate; //in case t cell public state(t cellstate) // ctor { this.genericstate = cellstate; } public override bool equals(object obj) { return genericstate.equals((obj state<t>).genericstate); } //never leaves }
and code of class cell, in never gets:
public class cell { public int row, col; public bool visited; public char value; public bool equals(cell other) //never gets here { return other != null && other.row == row && other.col == col; } }
i don't understand why never gets equal method of cell. wrong code?
the problem code not know t
has special method
bool equals<t>(t other)
it thinks should calling cell
's override of equals(object)
, code not override.
fixing simple: add iequatable<cell>
list of interfaces implemented cell
, , add constraint on t
ensure implements iequatable<t>
:
public class state<t> t : iequatable<t> { ... // rest of code remains same } ... public class cell : iequatable<cell> { ... // rest of code remains same }
Comments
Post a Comment