0
Two paramether class template
#include <iostream> using namespace std; template <class T> class Pair { private: T first, second; public: Pair (T a, T b): first(a), second(b) { } T bigger(); }; template <class T> T Pair<T>::bigger() { return (first>second ? first : second); } int main() { Pair <double> obj(23.43, 5.68); cout << obj.bigger(); return 0; } how can i make this code with two template. one int and the other double
3 ответов
+ 1
@Yusuf, since you declared only 1 template parameter you cannot instantiate the template with 2 arguments like this:
Pair <double, int> obj(23.43, 5);
cout << obj.bigger();
But you can make use of C++'s implicit casting rules - an int would implicitly cast to a double, so the following will work:
Pair <double> obj(23.43, 5);
//5 is an int
cout << obj.bigger();
0
You just create a different instance of your template with an int parameter:
int main()
{
Pair <double> obj(23.43, 5.68);
cout << obj.bigger();
Pair <int> iobj(22, 8);
cout << iobj.bigger();
return 0;
}
0
i mean that. one of two parameter will be double. and the other parameter will be integer at the same time
Obj (double,int)