class templates...

This is a discussion on class templates... within the C++ Programming forums, part of the General Programming Boards category; have i finally got the hang of templates ,,, so making a template class saves you from typing out more ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    319

    class templates...

    have i finally got the hang of templates ,,,
    so making a template class saves you from typing out more class's
    just so it can be a diferent type...
    Code:
    #include <iostream>
    using namespace std;
    
    
    template <class T>
    class Temp
    {
    public:
    T Varible; //Varible could be any data type..
    private:
    
    protected:
    };
    
    
    
    int main()
    {
    Temp<int> TemP;
    Temp<double> TemPP; //now the Temp data member Varible can be a double
    TemP.Varible = 40;
    cout<< TemP.Varible <<endl;
    cout<< "int " << sizeof(TemP.Varible) << "    " << &TemP.Varible <<endl;
    cout<< "double " << sizeof(TemPP.Varible) << "    " << &TemPP.Varible <<endl;
    //sizeof(TemP.Varible) <<endl;
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,339
    >> so making a template class saves you from typing out more class's
    It may save you typing, but that is never reason to use a template. Although along the same lines, if you use the same code with different types then using a template instead means that future changes need to be made in only one place and there is no chance of not updating all the necessary copies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  2. Class templates and iterators
    By creativeinspira in forum C++ Programming
    Replies: 2
    Last Post: 06-30-2007, 03:35 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21