0
Why templates are used in c++?
3 Réponses
+ 1
Sometimes you need to create same class for different types. For example you have a class that takes integer parameters only. But you need the class for float types as well. Instead of creating the same class twice and write code for both of them, you design your code accordingly by creating a template.
+ 1
template <class A, class B>
public static A sum(A x, B y) {
A a = A(y);
return a + y;
}
this will work for any two data types but will give the results expected only for numerical data types. you could now override it to make it much more efficient.
0
example please