qfedu-cpp-level/day7/d2.cpp

41 lines
587 B
C++
Raw Normal View History

2023-08-01 18:51:53 +08:00
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
template <typename T>
T &maxVal(T &a, T &b)
{
return a > b ? a : b;
}
class A
{
private:
int x;
public:
A(int x) : x(x) {}
void show()
{
cout << "x = " << x << endl;
}
public:
// 重载运算符>,解决函数模版的局限性,不用具体化函数模版
bool operator>(const A &a) const
{
return this->x > a.x;
}
};
int main()
{
A a1(20), a2(50);
// A &b = maxVal<A>(a1, a2);
A &b = maxVal(a1, a2);
b.show();
return 0;
}