CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2008
    Posts
    48

    When do we use Template classes

    Hi,
    Can anyone explain me when do we use Template classes ?

    Thanks

  2. #2
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: When do we use Template classes

    There are several cases where you can use template classes/methods :

    - templates can be used for static polymorphism.
    - templates can be used when an algorithm works on different types that meet certain criteria.
    - templates can be used for meta programming to optimize algorithms at compile time instead of runtime (template meta programming)
    - Guido

  3. #3
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    Re: When do we use Template classes

    Since you're apparently starting with templates, you can primarily think of using templates whenever you need to construct a class or an algorithm that works for any kind of data type: primitive types like int and double and/or user-defined ones. (This is basically a generalization of GNiewerth's second item.)

    Once you get over it you'll probably be interested on doing some fancy things. And then there's a whole new story that would relate to the other items mentioned by GNiewerth.

  4. #4
    Join Date
    Sep 2007
    Posts
    15

    Re: When do we use Template classes

    I'm using templates for a "super" class in a object-oriented DBMS. (Sounds more complex than it is)

    I can easily create different datatypes in the DBMS by creating a class that inherit the superclass like:

    Code:
    class stringProperty : property<string>
    {
    // some stringProperty methods etc..
    }
    So to sum it up, for me template classes are about code re-use. Why bother rewriting a class for another datatype when you can write "template<class T>" on top and replace the otherwise fixed datatypes with the T varable datatype. Then the class can be used for all datatypes/classes - pretty neat!

  5. #5
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: When do we use Template classes

    Its also worth noting that templates classes can be made using a non-type template parameter.

    Heres an example from my latest project.
    Code:
    template < uint N >
    class Sequence
    {
    private:
    IntVec sequence_;
    Sequence( const Sequence& ); 
    Sequence& operator =( const Sequence& );
    public:
    Sequence()
    {
    sequence_.reserve( N );
    for ( int i = 1; i <= N; ++i )
    {
    sequence_.push_back( i );
    }
    Shuffle();
    }
    
    const IntVec& GetSequence() const
    {
    return sequence_;
    }
    staticvoid Randomize( uint def = 0 )
    {
    if ( def == 0 )
    def = static_cast<uint>( std::time( 0 ) );
    std::srand( def );
    }
    
    void Shuffle()
    {
    std::random_shuffle( sequence_.begin(), sequence_.end() );
    }
    };
    


    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  6. #6
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: When do we use Template classes

    Hmmm seems msvc9 has awful cut/paste trouble. Gives fancy syntax colouring but cant preserve indentation grrr
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured