Find out what type my class is being converted to for comparisons

Matthew Rushworth nope at nopey.nope
Tue Oct 18 22:43:13 UTC 2022


On Tuesday, 18 October 2022 at 20:02:02 UTC, ag0aep6g wrote:
> On Tuesday, 18 October 2022 at 18:53:41 UTC, Matthew Rushworth 
> wrote:
>> The entirety of opEquals:
>>
>> ```
>> /// both matrices have to have an identical shape to compare
>> /// each element must be identical to match
>> bool opEquals(size_t X, size_t Y)(const Matrix!(X,Y) m1, const 
>> Matrix!(X,Y) m2) {
>>     for (int i = 0; i < m1.data.length; ++i) if (m1.data[i] != 
>> m2.data[i]) return false;
>>     return true;
>> }
>> ```
>
> A free-standing opEquals won't work. It needs to be a method of 
> the class. So:
>
> ```d
> class Matrix(size_t X, size_t Y = X)
> {
>     /* ... */
>
>     bool opEquals(const Matrix m2)
>     {
>         for (int i = 0; i < this.data.length; ++i)
>             if (this.data[i] != m2.data[i]) return false;
>         return true;
>     }
> }
> ```

Thank you, that worked perfectly, not sure exactly what I did 
wrong, I'm assuming I forgot to make the parameter a const 
variable


More information about the Digitalmars-d-learn mailing list