3) (Your code is solution to 3rd problem)
Use ternaries or std::tie:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//Usual convention is to return if left operand is less than right
//use const references as parameters
bool compare_less(const clockType& lhs, const clockType& rhs)
{
return lhs.h != rhs.h ? lhs.h < rhs.h :
lhs.m != rhs.m ? lhs.m < rhs.m :
lhs.s < rhs.s ;
}
//OR
#include <tuple>
bool compare_less(const clockType& lhs, const clockType& rhs)
{
return std::tie(lhs.h, lhs.m, lhs.s) < std::tie(rhs.h, rhs.m, rhs.s);
}
2) Yes, you need to create booloperator <(const clockType& a) — it is usual convention to define < operator first and define others through it.
If you are allowed, you can just delegate call to compare_less funtion.
1) function signature should be bool clockType::compare_less(const clockType& other) const
You can delegate call there too.
You need to return value of your function call from operator and compare3.
And I would prefer to have non-member operator, but it is just a preference in your case.
It is common in programming to call other functions implementing already existing functionality, as it helps to avoid code duplication and makes code easier to maintain.
In case if you want to implement comparison in each function: to compare two objects you need access to h,m and s variables, but ou cannot access them as they are private. So you have to delegate actual comparison to class facilities. This is encapsulation.