Dear all,
I have two template functions for displaying books and members respectively from arrays.
The function protoypes are:
and the functions implementation code is:
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
I have two template functions for displaying books and members respectively from arrays.
Code:
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]));
}
}
Code:
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]));
}
}
Code:
Member* get_member(Book* book);
Book* get_book(Member* member);
Code:
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;
}
Code:
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