C++
  Home arrow C++ arrow Page 4 - 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  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
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 / 42
    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 (contd.)


    (Page 4 of 9 )

    Now that we have created the function declaration for our template, lets create the actual code for it. In main.cpp, enter the following code:

    #include <iostream>

    #include "main.h"

    using namespace std;

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

    {

    return static_cast<T>((num1 + num2 + num3) / 3);

    }

    int main()

    {

    int x = 10;

    int y = 20;

    int z = 30;

    int returnValue = GetAverage(x, y, z);

    cout << "Returned " << returnValue << endl << endl;

    return 0;

    }


    We start out by #including the iostream head filer (so we can output to the screen) and our main.h header file, which contains the declaration of our template.

    Next, we have the full code for our template:

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

    {

    return static_cast<T>((num1 + num2 + num3) / 3);

    }


    The first line is exactly the same as the templates declaration in main.h. Our template only contains one line, which calculates the average of the three variables passed in as parameters, and returns that value.

    Our template uses the static_cast<>() function to make sure that the return value is also of type T. This is necessary, because most of the time, when finding the average, the result will contain a decimal point and mantissa. If, for example, we passed three int values to the template, then we would expect an int to be returned, and not a double, for example. That’s what the static_cast<>() function is responsible for handling.

    Note: The static_cast<>() function is actually a template. The value between the angled brackets tells the template what the return type of the template should be.

    Before moving onto the next section, try changing the types and values of x, y, and z. The return type of the template will be the same as the values passed to it!

    More C++ Articles
    More By Mitchell Harper


     

    C++ ARTICLES

    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion
    - First Steps in (C) Programming, continued
    - First Steps in (C) Programming, introduction
    - C++ Preprocessor: Always Assert Your Code Is...
    - C++ Preprocessor: The Code in the Middle
    - Programming in C
    - Temporary Variables: Runtime rvalue Detection







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway