我正在學習模板並遇到函數模板。我已經開發出了以下代碼,這些代碼是出於我的創造力或僅僅是好奇心。帶有運算符重載的函數模板
#include<iostream>
using namespace std;
template <typename type>
type Max(type a, type b)
{
return a > b ? a : b;
}
class Foo
{
public:
string s1;
/*
Foo& operator>(Foo &obj)
{
if (this->s1.length() > obj.s1.length() ? true : false)
return *this;
return obj;
}*/
};
int main()
{
cout << Max(2, 3) << endl;
cout << Max(2.0, 3.0) << endl;
Foo a, b;
a.s1 = "AB";
b.s1 = "ABC";
//cout<<Max(a, b).s1;
}
我的想法是通過富對象A和B使用最大函數模板和重載「>」操作者並以更大的長度打印對象的字符串。 我在正確的道路上?或者它應該與類模板相關?
'運營商>'正常返回'bool'。 – aschepler