源程序:
#include <iostream>
using namespace std;
template <class T>
class Sample
{
T n;
public:
Sample(T i)
{
n = i;
}
int operator ==(Sample &);
};
template <class T>
int Sample<T>::operator ==(Sample &s)
{
if (n == s.n)
return 1;
else
return 0;
}
int main()
{
Sample <int>s1(2),s2(3);
cout << "s1 与 s2" << (s1 == s2 ? "相等" : "不相等") << endl;
Sample <double>s3(2.5), s4(2.5);
cout << "s3 与 s4" << (s3 == s4 ? "相等" : "不相等") << endl;
system("pause");
return 1;
}
运行结果: