templates

This is a discussion on templates within the C++ Programming forums, part of the General Programming Boards category; what is a template and what is it used for?can you tell me the advanteges and disadvanteges?...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    Post templates

    what is a template and what is it used for?can you tell me the advanteges and disadvanteges?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    A template is a way to reuse code without having to worry about the types you use......so if you construct a neat container for ints and you want to reuse it for chars, then you dont have to rewrite the container.......for this to be effective though, the purpose of the template you create must not be effected by the types you wish to use...therefore they are great for containers (lists,stacks,queues..etc)

    Here's a slightly brief version of a stack that I can use with both ints and chars without having to change any of the template's code

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    
    template<class T>
    class MyStack{
    	struct Node{
    		T Data;
    		Node* Next;
    		Node(const T& newData,Node* newNext)
    			:Data(newData),Next(newNext){};
    	};
    	Node* Top;
    
    public:
    	void Push(const T& newData){Top = new Node(newData,Top);}
    	T Pop(){
    		Node *n = Top;
    		Top = Top->Next;
    		T RetData = n->Data;
    		delete n;
    		return RetData;
    	}
    	MyStack():Top(0){}
    	~MyStack(){
    		while(Top){
    			Node *n = Top;
    			Top = Top->Next;
    			delete n;
    		}
    	}
    };
    
    
    int main(){
    
    	MyStack<int> *m = new MyStack<int>;
    
    	cout << "Now for some numbers" << endl;
    	m->Push(10);
    	m->Push(15);
    	cout << m->Pop() << endl;
    	cout << m->Pop() << endl;
    
    	delete m;
    
    	MyStack<char> *n = new MyStack<char>;
    
    	cout << "Now for some chars" << endl;
    
    	n->Push('y');
    	n->Push('d');
    	n->Push('r');
    	n->Push('o');
    	n->Push('f');
    	cout << n->Pop();
    	cout << n->Pop();
    	cout << n->Pop();
    	cout << n->Pop();
    	cout << n->Pop();
    	cout << endl;
    
    	delete n;
    
    	return 0;
    }

  3. #3
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    A template is some code from which to start.. A tiny example is:
    PHP Code:
    int main()
    {
    return 
    0;

    This means that you won't have to write that when you start a new application.. but good templates are much much bigger...
    what does signature stand for?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 12:49 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-17-2005, 11:22 PM
  3. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 09:36 AM

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