Why struct opEquals must be const?

Rainer Deyke rainerd at eldwood.com
Mon Oct 18 12:43:29 PDT 2010


On 10/18/2010 09:07, Steven Schveighoffer wrote:
> What case were you thinking of for requiring mutablity for equality
> testing?

What about caching?

class C {
  private int hashValue = -1;
  // Mutators reset hashValue to -1.

  int getHash() {
    if (this.hashValue == -1) {
      this.hashValue = longExpensiveCalculation();
    }
    return this.hashValue();
  }

  bool opEquals(C other) {
    if (this.getHash() == other.getHash()) {
      return slowElementwiseCompare(this, other);
    } else {
      return false;
    }
  }
}


-- 
Rainer Deyke - rainerd at eldwood.com


More information about the Digitalmars-d mailing list