W
wongjoekmeu
Hello all,
I have a question about templates. Let say I have the following
template function
-----
template <class T>
T max(T a, T b)
{
return a > b ? a : b ;
}
-----
As far as what I understand if you make a statement as "max(2,3)", "T"
will be replace by "int" and a function like "int max(int a, int b)"
will be called. If I insert floats, then I will get "float max(float a,
float b)". As least that is what I understand from what I have read.
First of all I was wondering whether I am right or not. Now, let us
assume that I am correct. Let us now say that I have a class called
"Cat" and create two instances of "Cat" like "cat1" and "cat2".
Now what will happen if I make a statement like "max(cat1,cat2)" ????
will this evolves to the calling of a function like "Cat max(Cat cat1,
Cat cat2)" ??? If yes what does Cat1>Cat2 will return ? True ? or false
? To what will it evaluate.
Now suppose I have in the class a public member integer called, "age".
And in fact I meant to compare the age of the two cats yet keeping the
previous declaration of my function template. Can I overload my first
template function ??
Thank you very much in advance for answering my question.
Robert
I have a question about templates. Let say I have the following
template function
-----
template <class T>
T max(T a, T b)
{
return a > b ? a : b ;
}
-----
As far as what I understand if you make a statement as "max(2,3)", "T"
will be replace by "int" and a function like "int max(int a, int b)"
will be called. If I insert floats, then I will get "float max(float a,
float b)". As least that is what I understand from what I have read.
First of all I was wondering whether I am right or not. Now, let us
assume that I am correct. Let us now say that I have a class called
"Cat" and create two instances of "Cat" like "cat1" and "cat2".
Now what will happen if I make a statement like "max(cat1,cat2)" ????
will this evolves to the calling of a function like "Cat max(Cat cat1,
Cat cat2)" ??? If yes what does Cat1>Cat2 will return ? True ? or false
? To what will it evaluate.
Now suppose I have in the class a public member integer called, "age".
And in fact I meant to compare the age of the two cats yet keeping the
previous declaration of my function template. Can I overload my first
template function ??
Thank you very much in advance for answering my question.
Robert