[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppTemplateClass

(C++) Template class

A template class is a "class parameterized by types, values, or templates. The template arguments necessary to identify the class to be generated for the class template must be provided where a template class is used" [1].

template <class T>
struct MyClass
{
  T mValue;
};
int main() 
{ 
  MyClass<int> m; 
  m.mX = 10; 
} 


compile time assert

A template class can be used for a compile time assertion:

template <bool> struct CompileTimeAssert;
template<> struct CompileTimeAssert<true> {};
int main()
{
  CompileTimeAssert<((1+1)==2)> x;
  //The line below will give the following compile-time errors:
  //  [C++ Error] : Undefined structure 'CompileTimeAssert<0>'
  //  [C++ Error] : Size of 'y' is unknown or zero
  CompileTimeAssert<((1+2)==2)> y;
}


See compile time assert for details.

A template class is a type

In the examle below, a simple struct is defined, called Person with a template argument, a Mood, which is an enumeration. The example below shows that a Person<happy> cannot be converted to a Person<sad>, except by a dirty reinterpret_cast using a temporary pointer.

  1. include <iostream>
enum Mood{ happy, sad, angry }; template<Mood> struct Person; template<> struct Person<happy> { void sayHello() const { std::cout << "Hello " << std::endl; } }; template<> struct Person<sad> { void sayHello() const { std::cout << "Hello " << std::endl; } }; template<> struct Person<angry> { //An angry person cannot say hello }; template <Mood T> void SayHello(const Person<T>& person) { person.sayHello(); } int main() { //Create a happy person Person<happy> kristel; //Let the happy person say hello SayHello(kristel); //Luckily cannot cast a Person<happy> to Person<sad> : // Cannot cast from 'Person<0>' to 'Person<1>' //SayHello(reinterpret_cast<Person<sad> >(kristel)); sayHello(*(reinterpret_cast<Person<sad>* >(&kristel))); }


Comparing this with polymorphism, one notes that when using  ?CppTemplate, templates, types cannot be cast to each other. Using polymorphism this is legal using a dynamic_cast.

In other words, when using templates, a type is defined at compile time, whereas using polymorphism a type's behaviour can be changed at runtime.

Also note, that in the example above, the Person<angry> does not have a SayHello method and therefore can not be called. When using polymorphism either this (virtual) method would have to defined, or some other dirty casts would have been needed.

Reference



last edited (March 13, 2007) by bilderbikkel, Number of views: 2080, Current Rev: 5 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.