Topic : Templates
Author : Vijay Mukhi
Page : 1 Next >>
Go to page :



   The key to the future, according to Microsoft is COM or the Component Object Model.  Today, COM forms the core of all Microsoft technologies, and provides the skeleton for all it's future strategies. OLE (pronounced Olay) sits on top of COM and ActiveX sits on top of OLE. To most people ActiveX is nothing but a series of OLE classes.  If COM is so important then the entire world must learn it, or so you may say.  But this is easier said than done.  COM is extremely difficult to understand (if you attempt to learn it using the documentation provided by Microsoft)  The gist of COM, if stated in English could be put in the following words.  If you consider a Microsoft product like Visual Basic, you will find the concept of components.  These components may have properties and events and other attributes.  These components can be inserted into any container like VB, PowerBuilder etc. and we can integrate the component into our applications.  Coding a COM object was never a bother as long as we used
        Visual Basic. But the irony of the situation is that if  we wanted to do the same thing from C++, then we will have a problem. It was extremely difficult to use ActiveX from C++, even though the entire ActiveX, OLE, COM families are based on C++.  This was till the time Microsoft introduced Visual C++ 5.0
   Microsoft knew that they could not change C++ as a language, because they did not own it. They have added extensions to the language using their product Visual C++.  They have added some ingredients which allows them to stake a claim saying that C++ understands COM, or more specifically Visual C++ 5.0, the compiler understands COM.  In this tutorial we will attempt to analyze Microsoft's claim and try to rip apart their claims.  But before we do that we will educate ourselves about some of the lesser know aspects of C++.  
   If we are put in an un-envious position where we are told to write code which will be used to sort an array of ten numbers.  This is a simple task, usually the first lessons undertaken in any C/C++ class. If the instructor decides to add spice and asks us to print out these numbers, then this is no arduous task.  This is because we are aware that the first member of the array is stored at a[0], the next member at a[1] and so on.  Hence printing can be effected by using a[0], a[1] etc.  This task does not involve heavy overheads because we are aware of the fact that internally an array of chars, int's, or longs will be made up of one, two or four memory locations respectively.  This is a specific instance of an array of 'known' datatypes.   What would happen if the array contains instances of 'zzz', an arbitrary object.  Our sorting program will simply fail to take off.    
   The need of the hour is to be able to functions that will carry out the task of sorting irrespective of the datatypes involved.  Programs should have the ability to tackle any datatypes, be it of known nature like ints, chars or any other in-house datatypes like 'zzz'.  In other words we would like to produce code that is generic. Fortunately, C++ provides us with a tool that allows us to do just that.  We will first look at this aspect of C++ before answering the 'Question of the day' i.e. does VC++ 5.0 support COM.  To understand more about creation of generic code, we will look at some simple examples.

Program 1 :

#include <stdio.h>
class zzz
{
public:
   int i;
   zzz()
   {
   printf("in zzz...%d\n",sizeof(i));
   }
};
main()   
{
   zzz a;
}


You will realize that when we  say 'int i', i is not a variable but an an object which is an instance of a  class int. There is no difference between class int and zzz,  the compiler does not differentiate between the two. So wherever you can give a class, you can give an int, or you could give  'zzz',  they are all the same. The problem is that by saying that i is an int, we are laying down a single rule that henceforth because i is an int , it cannot  be anything else.  However 'C' realizes this, not at the time when the code is being typed in, but at time of compiling the code.  When the compiler encounters the 'i', he has to be aware of the class-type of 'i', without such a knowledge of this the compiler cannot allocate memory nor can it invoke the constructor.  Delaying this information, from being supplied at coding time to the time when compilation starts, would mean availing the flexibility of having code which can work with any datatypes.  This is the basic tenet of the Template Classes. They allow us to defer the declaration of the datatype to the time where the compilation starts.  
Create the following program as 'Console Application' using MS-DEV. Using 'Console Application' allows us to use the keyword 'main'

Program 2 :

#include <stdio.h>
template <class T> class zzz
{
public:
   T i;
   zzz()
   {
      printf("in zzz...%d\n",sizeof(i));
   }
};

main()
{
   zzz <int> a;
   zzz <char> b;
}


   In the above program instead of stating that 'i' is an object that looks like 'int', we say that it now resembles 'T'. So when we are writing the code, we am not making any assumption regarding the datatype of 'i'.  The datatype could be a 'zzz', it could be a 'yyy', it could be anything under the sun( or for that matter anything above the sun can be included too). If we can do this, then when we say Alt-B B, C++ should know whether 'i' is an int or a char or a long. Depending on the data that we choose to supply, C++ will decide the data type of 'i'. That  means when we get the obj file, there will be no T. Had we said 'int i', then this goes to the obj file and a certain amount of memory is allocated for i.  Instead  we have chose  to use 'T' which allows us to use any datatype . Though this assumes the dimension of using a glorified and  extremely powerful macro, this is not true because in a macro you don't have such a degree of flexibility. Comparisons between a macro and the using the Template Classes are uncalled for.  Because when we use the template classes, just before the compilation takes place, somebody comes in and replaces all T's  with the datatype that we have given.  This is not possible in case of the macro.  Using the template classes is more than using the services of the preprocess.

In the above program instead of saying class 'zzz', we have used  a template   <class T> and then we  write class zzz. Whenever you say 'zzz a', the constructor gets called, and constructor can get called with parameters. You will get an error message because now instead of saying 'zzz a', you will have to say 'zzz<int> a'.When C++ reads this line. The 'T' in the class definition become an 'int'. So  wherever he sees T, it will replace it with int. That means there is an extra stage in the compilation process now.  At this juncture if you choose to write 'zzz<char> b', C++ will create another class internally. Hence internally there will be no T, there will be an int and a char. In that sense this has lead to the creation of two different classes. For understanding purposes you can call it as zzz1 and zzz2.
The resident wag among us tells us that this is akin to using a typedef.  However a  typedef is like a '#define'.  So you cannot equate the two concepts, because a typedef is very static. A typedef is used for conveying the message that wherever the compiler encounters a 'T' it should be replaced by an int.  However in the programs given above we are replacing one instance of 'T' with an int whereas the other 'T' is replaced by a 'char'.  We challenge anyone to do the same using typedef's and without involving any further overheads.  To silence our critics we will go a step further.

Program 3 :

#include <stdio.h>
template <class T,class U> class zzz
{
public:
   T i;
U k;
   zzz()
   {
      printf("in zzz...%d\n",sizeof(i));
      printf("in zzz...%d\n",sizeof(k));
   }
};
main()
{
   zzz <int,char> a;
}


In zzz we have used two parameters.  Though this increases the complexity of the program.  It serves as a good learning experience for may of us.  Unlike in the preceding program where we have used 'known' datatypes, in the following program we have  used a class as the parameter to the template class.

Page : 1 Next >>