Home arrow C++ arrow Page 8 - The Mighty C++ Template

The Mighty C++ Template

One of the biggest advantages of using C++ is templates. Templates were designed from the ground up to allow developers to write one function to handle many different types of parameters. In this article, Mitchell will explain what both function and class templates are, and give examples of each.

Author Info:
By: Mitchell Harper
Rating: 5 stars5 stars5 stars5 stars5 stars / 48
November 23, 2001
  1. · The Mighty C++ Template
  2. · Understanding function overloading
  3. · The template
  4. · The template (contd.)
  5. · A simple class template
  6. · A simple class template (contd.)
  7. · Instantiating the class
  8. · Behind the scenes
  9. · Conclusion

print this article

The Mighty C++ Template - Behind the scenes
(Page 8 of 9 )

You might be wondering just exactly what goes on under the hood when a C++ class template is instantiated.

Firstly, the C++ compiler creates an instance of the template and stores it on the stack. Because our class template passes integer values to its constructor, the compiler creates an instance of our class (on the stack) that can work with variables of type int. This leads us to an important point: The class template itself doesn’t actually do any of the code processing. It simply creates a new instance of the class and its encapsulated functions for the type of variable that it is dealing with, such as int, float, char, string, etc. If an instance of a class for a specific variable type has already been created, the compiler will use that class to create the new instance. When the program ends, the class template and all of its copies are popped off the stack. Just remember that the template is only responsible for creating an instantiation of the class for the type of variable that is required by the program code and doesn’t actually get executed.
blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- 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++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials