Thread: Templates

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    25

    Templates

    I have a template queue class and I need to define it in another class which isn't templated how do I go about doing that? Do I need to make all classes using the Queue template classes?

    a little example:

    Code:
    template <class T>
    class Queue{
        .
        .
        .
    };
    
    
    class School{
    
    public:
       . 
       .
    private
       Queue<T> firstGraders;
    };
    this is what I would like to do and School would be declared in main

  2. #2
    Registered User
    Join Date
    Dec 2006
    Posts
    8
    If you want School to be a template you have to declare it as such and the data type passed to School will be passed to Queue:

    Code:
    template<class T>
    class School{
    
    public:
       . 
       .
    private
       Queue<T> firstGraders;
    };
    If you do not want School to be a template you have to instantiate Queue with an actual type in School:

    Code:
    class School{
    
    public:
       . 
       .
    private
       Queue<Student> firstGraders; // Student class defined somewhere else 
    };

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    Thanks

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, 01:49 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  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