Several questions, tutorial is being vague...

[It's 1:49 AM here right now so sorry if these are stupid questions >_<]

Here's the program in question, copied straight from the tutorial:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

template <class T>
class mypair {
    T a, b;
  public:
    mypair (T first, T second)
      {a=first; b=second;}
    T getmax ();
};

template <class T>
T mypair<T>::getmax ()
{
  T retval;
  retval = a>b? a : b;
  return retval;
}

int main () {
  mypair <int> myobject (100, 75);
  cout << myobject.getmax();
  return 0;
}


I'm confused about several things. For one, I'm not quite sure I understand the scope operator. Why is it necessary to include template <class T> before getmax's definition? I would have thought that the template before mypair's definition would be sufficient, but apparently not. Seems a bit weird to me. I don't see a template argument for the getmax function so I'm assuming that the template argument for mypair in line 22 affects the template parameters in getmax. Or something. To be honest I don't have a clue what's happening in lines 13-19 and I don't think I'm going to get anywhere on this myself. Seriously, what's going on? All those T's are confusing me @_@
Strange they start with templates in classes...

To be honest, I'm not really familiar with templates either, but I will try to explain it to you by a normal funcion that does the same thing as your class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

template <typename T> 
inline T const& max (T const& a, T const& b) 
{ 
    // if a < b then use b else use a 
    return a<b?b:a; 
} 

int main(){
   double a=3,2;
   double b=4,7;
   cout<<max(a,b);
   int c=9;
   int d=1242;
   cout<<max(c,d);
   cin.ignore();
   return 0;
}


As you can see, whe create a function that returns the highest value of two variables. Nothing strange about that. But in a normal function, you would declare the type of the return value and the two parameters. Here we first create a template "T" and then use that as type.
This means that we can use every datatype (including self-defined classes) that support the operations we do with them inside the function, in this case ">".
So whe call the function, first with two doubles, then with two integers. You could also call it with strings and floats, etc. etc.

For so far template functions. Now about your code: the reason that you have to declare T twice is because it belongs to the fowolling function (or class).
I dont know or you understand this: retval = a>b? a : b; The "?" is an operator with three operants. The first, on the left ("a>b"), gives true or false. If it gives true, the second ("a") will be 'exucuted', in this case stored into retval. If the first operant holds false, the tird operand will be executed ("b").\
mypair <int> myobject (100, 75); Here we create an object of our templateclass, using the self-defined constructor. With <int> the compiler is explecitly told that T is an integer. If you didnt wrote this, T would become of type integer inplecitly the first time you use it (when you use 100 as argument).

Hope this helps. If you got any questions left, please ask.
Last edited on
Consider if our code looked like this - what would happen??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

template <class T>
class mypair {
    T a, b;
  public:
    mypair (T first, T second)
      {a=first; b=second;}
    T getmax ();

};

//template <class T> //What would happen if we left this bit out??
T mypair<T>::getmax ()
{

  int retval;
  retval = a>b? a : b;
  return retval;
}

int main () {
  mypair <int> myobject (100, 75);
  cout << myobject.getmax();
  return 0;
}


When a C++ compiler comes to line 14 - it would expect a return type (for example int or double or such like.)
It would not know what 'T' was and would give an error - about 'T' not being a type.
By prefixing the function with template <class T>, the compiler knows it's
dealing with a template function and 'T' is the typename place-holder.
Like Scipio said, I'm surprised you are doing/learning about template
classes before doing/learning template functions, because this way of writing
the functions for a template class is the way it is written for a normal template function (execpt of course that functions for template classes contain the classname:: scope operator.)
Well this isn't nearly as hard as it seemed last night. Guess that's what I get for staying up >.> To clarify I read function templates first and I understand the concept behind templates but the template placement is a bit confusing.

I don't really understand the <T> in line 15. I know what it's for, but why is it there? Why would you specify <T> anyway, the compiler doesn't really know what that is, right? Shouldn't something like <int> be specified?
Shouldn't something like <int> be specified?

No. Even if that was valid syntax, which it isn't, that would defeat the purpose of having a single function definition that worked for all types. What if you then went on and created an instance of mypair<double>?
And yes. The compiler knows what T is. You told it what it is.
Last edited on
Meh. All I know is that the <T> is required but I don't know how it pencils out...
That's fine. Syntax is arbitrary, anyway. It's not like there's a mathematical foundation upon which a given syntax is based.
Heh, I noticed that. When I'm learning something I have a hard time swallowing anything with too many, ahem, variables. (No pun intended. Really.)
T mypair<T>::getmax()

without the template< class T > in front of it would look to the compiler like any ordinary member function implementation with user-defined type T.

template< class T > tells the compiler that in the

T mypair<T>::getmax()

line, T should not be taken literally, but rather substituted for a real type that mypair<> was instantiated with.

It's just telling the compiler there is an extra level of type indirection... T is not the actual data type, but it's just a placeholder for the actual one.

Yeah...T is used in templates sort of like how i, j, k etc are used for loop variables.
Have a look at http://www.cprogramming.com/tutorial/templates.html as well, might help out a bit.
Topic archived. No new replies allowed.