I have a class called IntMatrix
which has 2 fields:
Dimensions dimensions;//To save height and width of my matrix
int *data;//For saving data
and I need to override the operators >,>=,<,<=,==,!=
which take a scalar and compare each single value in my matrix with that scalar accordingly, and returns a new matrix with the same size that contains 1 if the comparison returned true and 0 else.
For example:
mat: {1,2,3;4,5,6}
mat_2 = mat > 3;//Should return {0,0,0;1,1,1}
So, you may notice that all my functions have the exact lines of code except whenever I wrote !=
it should be > or < or == or etc...
.
So, in order to remove duplication here's how I implemented them:
IntMatrix IntMatrix::operator<(int num) const {
return filter(*this,Between(INT_MIN,num-1));
}
IntMatrix IntMatrix::operator>(int num) const {
return filter(*this,Between(num+1,INT_MAX));
}
IntMatrix IntMatrix::operator!=(int num) const {
return filter(*this,Between(num,num), true);
}
where Between
is a functor (to replace a pointer to a function).
So, when I reviewed my code I strongly believe this is not the best solution to give to this kind of problem, since filter()
won't be well understandable by other programmers.
From your experience is there anyway to improve this code?
(I know that macros would be perfect here but don't want to use them either since they are a bad practice)
Note: I am working with C++11 and want only to use standard libraries.
<
and==
that return something else other than bool. It might cause issues on some level. In Matlab they have special bitwise operators but it will be hard to do in C++. Consider making functions instead of operators. \$\endgroup\$