Thread: Quick question about class template

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    14

    Quick question about class template

    Hi@all

    I wrote my own list class now I am supposed to convert it to a template class so I can use it for another program.

    In my list I also have an iterator class which I implemented.

    At the begining of .h file, I put
    Code:
     template <typename Item>
    and I changed functions that I wrote for list class like for copy constructor
    Code:
    list (const list<Item>& orginal)
    my question is do I need to change anything for iterator class? I have an example template class which doesn't have an iterator class in it so I am confused

    thanks

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    template <typename Item>
    I think it's pretty standard to use "T" for the type parameter, i.e.:

    template <typename T>

    "Item" isn't too descriptive, and in fact it's probably misleading. "T" stands for type, and you are saying that wherever the "T" appears in your code, it will be a variable.

    I wrote my own list class now I am supposed to convert it to a template class
    What type did you write it for? It's pretty simple to convert, you just replace that type with "T" wherever you want the type to be a variable.
    ---

    Suppose you declare a list like this:

    list<int> myList;

    If you want to declare an iterator for that list, you do this:

    list<int>::iterator pos = myList.begin();

    As you can see, the type of the things stored in the list is reflected in the iterator type as well: 'pos' is of type list<int>::iterator
    Last edited by 7stud; 12-06-2005 at 09:38 PM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    Thanks 7stud

    I understood everything but how about if I have a friend iterator class in my template class, I mean when I convert my class to a template class, do I have to change my iterator friend function?

    for example; if I have my own stack class and It has a friend iterator class and I want to convert it to a template class, do I need to change iterator class too?

    Code:
    template <typename T>
    class Stack{
    friend class iterator;
    private:
    .....
    ......
    public:
    //************************begin class iterator**************************
    class iterator {
    friend class Stack;
    private:
    .....
    .....
    public:
    .....
    .....
    };
    //************************end class iterator**************************
    private:
    .....
    .....
    public:
    ....
    .....
    };

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1)What type in your class do you want to vary?

    2)Post how you would declare an iterator for your list class

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    Quote Originally Posted by 7stud
    1)What type in your class do you want to vary?

    2)Post how you would declare an iterator for your list class
    Thanks for your help but I can not post my code because of some reasons.

    If I knew how to convert my class to a template class, I would complete my program.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I didn't ask you to post your code. I asked you two simple questions. If you can't answer them, then you can't write templates.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. help with template class using a template node
    By aciarlillo in forum C++ Programming
    Replies: 11
    Last Post: 06-02-2005, 05:46 PM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. question about .net to 6.0 change causing errors
    By jverkoey in forum C++ Programming
    Replies: 17
    Last Post: 03-23-2004, 10:45 AM
  5. Operator overloading in template classes
    By moejams in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2003, 05:16 PM