#include <string> #include <iostream> template <typename T> T max(T a, T b) { return a > b ? a : b; } class Dummy { private: std::string name; int age; public: Dummy(int an_age) {age = an_age;} bool operator> (Dummy &a) {return age > a.age;} std::string toString() const {return "The age is " + age;} }; std::ostream& operator<<(std::ostream& out, const Dummy& d) {return out<< d.toString();} int main() { std::cout << max(3, 7) << std::endl; std::cout << max(3.0, 7.0) << std::endl; std::cout << max<int>(3, 7.0) << std::endl; std::cout << max("hello", "hi") << std::endl; Dummy d1(10); Dummy d2(20); std::cout << max(&d1, &d2) << std::endl; return 0; } I'm pretty new to C++ but not new to programming. I've written the code to play with template and operator overloading in C++.
It took quite a while to make it compile and partially work.
The ostream operator<< is not working properly, only to return the address of the object. I can't figure out the causes.
I managed to make it compile by blind trial and error, so I suspect the code might be broken to some extent. And I may not be aware of what'd be improved.