Question

No matter how hard I search I can not figure this out. I have to do a project with overloading operators and the only two I am having trouble with are == and !=. For the project directions == is defined as the rows, columns, and values of both matrices being identical.

so far for the == all I have is this:

bool Matrix::operator==(const Matrix & m) {
// not sure what to return if all values must be equal
}

Question is: what do I return and compare in the function for this to work properly?

Thanks ahead of time.

Was it helpful?

Solution

In mathematical terms, matrix equality requires all corresponding elements of two matrices, A and B have the same value. Thus A = B if and only if Ai,j = Bi,j for all i, j.

In your case, where you are looking at implementation in C++, you will need to verify the Matrix instances this and m are the same size, and they each contain the same value at corresponding locations. If they are the same size and have the same values in the corresponding locations, then == will return true (false otherwise), and != will return false (true otherwise).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top