C++
  Home arrow C++ arrow Page 3 - Templates in C++
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 
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++

Templates in C++
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 5
    2008-06-17

    Table of Contents:
  • Templates in C++
  • Starting with Templates
  • Class Templates
  • Template Specialization

  • 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


    Templates in C++ - Class Templates


    (Page 3 of 4 )

    The same can be done for a class. So let's create it.

    template< class T>

    class ClassTemp

    {

    public:

    ClassTemp();

    ~ClassTemp();

    T& member();

    const T& member() const;

    private:

    T m_member;

    typename T::C(y);

    typedef char C;

    ClassTemp::C d;

    };

    template<class T>

    T& ClassTemp::member()

    {

    return m_member;

    }

    template<class T>

    const T& ClassTemp::member() const

    {

    return m_member;

    }

    The code snippet presented above reserves more information about classes. First of all, you have to implement the functions in the header file also. So you can't use the usual method of making the declarations in the header and writing the implementation in the source files (cpp files). All of this must be within a single file.

    Is this a drawback of the compiler? It seems weird to me that even Microsoft failed to implement this. Until they do, remember to write the declarations and the definitions in the same file.

    Template functions and classes usually take up more space; they are larger, but for the sake of programming, they aren't less efficient compared to one written for a specific type. Moreover, if you do it right, they may make the difference because they can be compiled as inline functions. Heaven on earth!

    Moreover, there is an additional key you should know about it. It's something that occurs when the compiler fails to decide whether you're declaring a function or a type within the parameters of the function. If this is the case, you can explicitly specify that it's a key with the typename keyword. This should be used each time you have a qualified name that refers to a type and depends on a template parameter. Only use the keyword typename in template declarations and definitions.

    To more easily understand the issue, let's revisit the declared class. You see, should we write just ClassTemp::C then the ClassTemp is ill-formed, or more exactly it depends upon a parameter. To bypass this limitation, write the typename before the expression to indicate that it points to a type.

    Also, this is the situation in the case of the ClassTemp::x(y). Now this can be interpreted as we call a function of the ClassTemp with parameter y or as we declare a y variable of ClassTemp::x. The compiler will take the function interpretation, but if we want it any other way, we can just add the typename keyword beforehand.

    More C++ Articles
    More By Gabor Bernat


       · Templates are reminding you till a point about the for statement. Back in the early...
     

    C++ ARTICLES

    - Paths and Files
    - Directories in C++
    - Focusing on C++ Files
    - Const Correctness in C++
    - Manipulating Streams and Files with C++
    - Streams and Files
    - 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






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT