0
点赞
收藏
分享

微信扫一扫

c++ 泛型编程

JakietYu 2023-01-16 阅读 141


​​link​​

#include <iostream>

using namespace std;

template < typename T >
void Swap(T& a, T& b)
{
T t = a;
a = b;
b = t;
}

template < typename T >
class Op
{
public:
T process(T v)
{
return v * v;
}
};

int main()
{
int a = 1;
int b = 2;

Swap(a, b);

cout << "a = " << a << ", " << "b = " << b << endl;

double c = 0.01;
double d = 0.02;

//Swap<double>(d, c);
Swap<double>(d, c);

cout << "c = " << c << ", " << "d = " << d << endl;

Op<int> opInt;
Op<double> opDouble;

cout << "5 * 5 = " << opInt.process(5) << endl;
cout << "0.5 * 0.5 = " << opDouble.process(0.5) << endl;

return 0;

}


举报

相关推荐

0 条评论