I've never used operator overloading before and on one of my assignments it says:
"Use polymorphism only as appropriate. The general rule is that if an inbuilt operator matches the purpose of a member function then it should be overloaded."
I've managed to get both versions to work and I think that the 2nd version of ApplyFilter is better. but by me using operator overloading does it make the code a bit harder to read?
Non-Overloaded
int TheFilter::ApplyFilter(TheData& dataIn, TheData& dataOut) {
// other stuff here.
for (int i = 0; i < dataOut.length(); i++) {
dataOut.set_values(i, 0);
for (int j = 0; j < length(); j++) {
dataOut.set_values(i, ( dataOut.get_values(i)
+ (dataIn.get_values(i+j) * get_values(j)) ));
}
}
}
Overloaded
int TheFilter::ApplyFilter(const TheData& dataIn, TheData& dataOut) {
// other stuff here
for (int i = 0; i < dataOut.length(); i++) {
dataOut[i] = 0;
for (int j = 0; j < length(); j++) {
dataOut[i] += dataIn[i+j] * values[j];
}
}
return OK;
}
EDIT - The Data Class that I was using for the overloaded version!
class TheData {
public:
int length()
double& operator[] (int index);
const double& operator[] (int index) const;
void print();
void resize(int);
private:
std::vector<double> values;
bool valid;
};