Templates

This is a discussion on Templates within the C++ Programming forums, part of the General Programming Boards category; I have a template queue class and I need to define it in another class which isn't templated how do ...

  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, 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