Can I use 1 template function instead of 2?

This is a discussion on Can I use 1 template function instead of 2? within the C++ Programming forums, part of the General Programming Boards category; Dear all, I have two template functions for displaying books and members respectively from arrays. template<class Object> void List<Object>::displayBook(char* type) ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    16

    Question Can I use 1 template function instead of 2?

    Dear all,

    I have two template functions for displaying books and members respectively from arrays.

    template<class Object>
    void List<Object>::displayBook(char* type)
    {
    if (num_elements == 0)
    cout<<"No "<<type<<" is found in the "<<type<<" array.\n";
    else
    for(int element=0; element<this->num_elements; element++)
    {
    cout<<'\n';
    this->element_list[element]->display(association_list.get_member(element_lis t[element]));
    }
    }


    template<class Object>
    void List<Object>::displayMember(char* type)
    {
    if (num_elements == 0)
    cout<<"No "<<type<<" is found in the "<<type<<" array.\n";
    else
    for(int element=0; element<this->num_elements; element++)
    {
    cout<<'\n';
    this->element_list[element]->display(association_list.get_book(element_list[element]));
    }
    }

    The function protoypes are:
    Member* get_member(Book* book);
    Book* get_book(Member* member);

    and the functions implementation code is:

    template<class Book,class Member>
    Member* AssociationList<Book,Member>::get_member(Book* book)
    {
    Member* member=0;
    bool searching=true;
    int index=0;

    while(searching)
    {
    if (this->association_list[index])
    if (this->association_list[index]->linked_book()==book)
    {
    member=this->association_list[index]->linked_member();
    searching=false;
    }
    else
    index++;
    else
    index++;
    if (searching && (index == LIST_SIZE))
    {
    searching=false;
    }
    }
    return member;
    }

    template<class Book,class Member>
    Book* AssociationList<Book,Member>::get_book(Member* member)
    {
    Book* book=0;
    bool searching=true;
    int index=0;

    while(searching)
    {
    if (this->association_list[index])
    if (this->association_list[index]->linked_member()==member)
    {
    book=this->association_list[index]->linked_book();
    searching=false;
    }
    else
    index++;
    else
    index++;
    if (searching && (index == LIST_SIZE))
    {
    searching = false;
    }
    }
    return book;
    }


    Could someone tell me how can I use One Template Function Instead of Two (if this is possible)?

    Apologies for posting the similar question many times.
    grscot

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    94
    By looking at your fnct's, I dont think you have really grasped the concept of templates.

    The purpose of a template is that the type of data the fnct receives is unkown. You are working with objects and a object can contain anything inside of it. For example:
    Code:
    template <class T>
    void SLList<T>::makeLink (const T &nData)
    {
    	if (head==NULL)
    	{
    		head=new SLList<T>;
    		head->nodeData=nData;
    		head->next=NULL;
    		tail=head;
    	}
    	else
    	{
    		tail->next=new SLList<T>;
    		tail=tail->next;
    		tail->nodeData=nData;
    		tail->next=NULL;
    	}
    }
    will create a linked list and fill it, but the important thing to note is that it is receiving nData which is an object that can contain data of any type e.g. char, int, float and maybe even all of them.
    Code:
    void List<Object>::displayBook(char* type) <--- type known
    void SLList<T>::makeLink (const T &nData)  <--- type unknown
    Back to your question, by creating a proper template linked list class you will be able to retrieve information from the one function. I think you should seriously re-think your strategies and try to implement the class as a template.

    One more thing....pls use code tags around your code. It makes it alot easier to read and understand.


    Sophie
    Last edited by sweets; 04-30-2003 at 10:29 AM.
    simple is always an understatement.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. function template question
    By Thantos in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2004, 10:40 AM

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