commutativity

Commutative operator overloading + of 2 different objects

青春壹個敷衍的年華 提交于 2019-11-28 03:50:40
问题 I have 2 classes which represent a matrix: 1. RegularMatrix - O(n^2) representation 2. SparseMatrix - a matrix that is represented as linked list (without zeros). lets say i have: RegularMatrix a; SparseMatrix b; i want to be able to do: a+b; and also: b+a; so i'm overloading the + operator. My question is, since I want the addition to be commutative (a+b = b+a), do i need to implement 2 overloadings, one for each case? RegularMatrix operator+(const RegualarMatrix &, const SparseMatrix &);