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_list[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 prototypes 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 -
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_list[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 prototypes 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.
grscottry implement func^n overloading/overriding