C++クラステンプレート、関数テンプレート

503 ワード

関数テンプレート
関数テンプレートはjavaの汎用メソッドに相当します
template 
T test5(T i, T j){
	return i > j ? i : j;
}

void main(){
	double f = test5(13.2, 15.3);
	cout << f << endl;
}

印刷結果15.3
クラステンプレート
クラステンプレートはjavaの汎用クラスに相当します
template < class T,class E>
class Q {
public:
	T test(T t, E e){
		return t + e;
	}
};

void main(){
	Q q;
	cout << q.test(1,1.1) << endl;
}  

印刷結果は2