What is getting me confused:
Alex, the author of Learncpp.com writes about this example: "The MinMax class keeps track of the minimum and maximum values that it has seen so far. We have overloaded the + operator 3 times, so that we can add two MinMax objects together, or add integers to MinMax objects."
I don't inderstand the "3 times" part. Could someone explain which 3? Because in the addition sum in main(), i see more then 3 addition sums. (i count 5 addtion sums)
Also the code at line 30 has me baffled: return MinMax(nMin, nMax);
Shouldn't it be returning an addition function, like at line 47?
class MinMax
{
private:
int m_nMin; // The min value seen so far
int m_nMax; // The max value seen so far
public:
MinMax(int nMin, int nMax)
{
m_nMin = nMin;
m_nMax = nMax;
}
int GetMin() { return m_nMin; }
int GetMax() { return m_nMax; }
friend MinMax operator+(const MinMax &cM1, const MinMax &cM2);
friend MinMax operator+(const MinMax &cM, int nValue);
friend MinMax operator+(int nValue, const MinMax &cM);
};
MinMax operator+(const MinMax &cM1, const MinMax &cM2)
{
// Get the minimum value seen in cM1 and cM2
int nMin = cM1.m_nMin < cM2.m_nMin ? cM1.m_nMin : cM2.m_nMin;
// Get the maximum value seen in cM1 and cM2
int nMax = cM1.m_nMax > cM2.m_nMax ? cM1.m_nMax : cM2.m_nMax;
return MinMax(nMin, nMax);
}
MinMax operator+(const MinMax &cM, int nValue)
{
// Get the minimum value seen in cM and nValue
int nMin = cM.m_nMin < nValue ? cM.m_nMin : nValue;
// Get the maximum value seen in cM and nValue
int nMax = cM.m_nMax > nValue ? cM.m_nMax : nValue;
return MinMax(nMin, nMax);
}
MinMax operator+(int nValue, const MinMax &cM)
{
// call operator+(MinMax, nValue)
return (cM + nValue);
}
int main()
{
MinMax cM1(10, 15);
MinMax cM2(8, 11);
MinMax cM3(3, 12);
MinMax cMFinal = cM1 + cM2 + 5 + 8 + cM3 + 16;
std::cout << "Result: (" << cMFinal.GetMin() << ", " <<
cMFinal.GetMax() << ")" << std::endl;
return 0;
}
Ahh, i see, so when he says: "We have overloaded the + operator 3 times" what he is actually saying (in my world) is something like: "we have defined 3 scenarios under which the + operator will be overloaded and how the compiler should react." Is that right?
About the bad code: I'm a beginner and am keen to learn correct coding, could you please outline what is wrong with Alex's code?
Mainly ambiguity of operator+. What minmax + int means? Does it add int to both minimum and maximum? Does it adds that int to set of values you are finding max and min from?
Using common arithmetical operators to denote some special behavior can be surprising to users. If you need operators to reduce visual clutter to comon operations, a bitwise operators often used, especially bitwise_or aka pipe (boos::range composition). Other possibilities are overloading operator() (boost::accumulators) or other operator which has at least some logical connection to operation: / and /= for filename path contacenation (boost::filesystem), % as format specifier (boost::format, but I do not think this was a good choice).
Think of it this way: you have three different functions that all do completely different things, and yet for some reason you decide to give them the same name. Now your code using them is confusing to read because you have to reason about each use and try to figure out which overload is being used. It's a complete mess.
You should only overload a function with one that does the same thing in a slightly different manner, not when it does a completely different thing.
you have three different functions that all do completely different things
Actually they do the same thing: updating minimum and maximum of set of values to reflect minimum and maximum of new union of several sets. Still, plus is not the best choice because of ambigity.
Though, operator+ is not ideal: it usually implies some kind of interval union (For example, boost ICL implements operators + and += in terms of aggregated set union.)
If we provide for implicit conversion from 7 to [7] ie. [7 .. 7], a single overloaded operator would suffice.