C++
  Home arrow C++ arrow Page 3 - The Mighty C++ Template
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

The Mighty C++ Template
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 43
    2001-11-23

    Table of Contents:
  • The Mighty C++ Template
  • Understanding function overloading
  • The template
  • The template (contd.)
  • A simple class template
  • A simple class template (contd.)
  • Instantiating the class
  • Behind the scenes
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    The Mighty C++ Template - The template


    (Page 3 of 9 )

    If you’ve ever programmed with Visual Basic, then you will probably be familiar with the variant data type. The variant is type-less in a sense that it allows a variable to be declared as a wild card and contain any type of data.

    Well, C++ doesn’t allow this, and in many ways, templates can substitute for variants. To fully understand templates, let’s start with an example. Open your favourite C++ IDE, and create a new project with two files: main.h and main.cpp. Into main.h, enter the following code:

    template<class T> T GetAverage(T num1, T num2, T num3);

    This is our template declaration. It looks like a regular function declaration and its purpose is exactly the same as any functions declaration: to let the compiler know that the template exists. Let’s break down the templates declaration:

    template<class T>

    This section of the declaration tells the C++ compiler that we are defining a new template definition. Between the angled brackets, we are declaring a new type identifier, T. This tells the compiler to create a new type identifier called T. T will hold the type of variable that this template will be working with. The type of variable is decided from the types of each of the parameters passed to the function. If, for example num1, num2 and num3 were of type int, then T would be an int.

    T GetAverage(T num1, T num2, T num3);

    Let’s step away from templates for a moment and imagine the following function declaration:

    int GetAverage(int num1, int num2, int num3);

    Looks similar to our template definition right? Well it is. The T is acting as the type of variable that the template will work with. The template doesn’t know what type of variable it can accept yet. As long as the variables passed to the function can handle the operator+ and operator/ (can be added with/divided by other variables) functions (which all numeric types can), then the function will work successfully and will return the sum of all of the numbers divided by three (the average).

    More C++ Articles
    More By Mitchell Harper


     

    C++ ARTICLES

    - More Tricks to Gain Speed in Programming Con...
    - Easy and Efficient Programming for Contests
    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek