1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#include <iostream>
#include <iomanip>
#include <functional>
struct MyEqualTo
{
template <typename T>
bool operator()(const T& lhs, const T& rhs)
{
return (lhs == rhs);
}
};
template <class F, typename T>
class CompareObject
{
public:
CompareObject(F f, const T& targetValue, const T& initValue) :
f(f), target(targetValue), val(initValue) {}
bool compare() { return f(val, target); }
void setValue(const T& newValue) { val = newValue; }
private:
F f;
const T target;
T val;
};
int main()
{
std::cout << std::boolalpha;
//CompareObject oc1(std::equal_to<int>{}, 10, 0);
CompareObject<std::equal_to<int>, int> oc1(std::equal_to<int>{}, 10, 0);
std::cout << "Is 10 == 0? " << oc1.compare() << std::endl;
oc1.setValue(10);
std::cout << "Is 10 == 10? " << oc1.compare() << std::endl;
//CompareObject oc2(MyEqualTo{}, 5, 0);
CompareObject<MyEqualTo, int> oc2(MyEqualTo{}, 5, 0);
std::cout << "Is 5 == 0? " << oc2.compare() << std::endl;
oc2.setValue(5);
std::cout << "Is 5 == 5? " << oc2.compare() << std::endl;
return 0;
}
|