Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I use 1 template function instead of 2?

Status
Not open for further replies.

grscot

Programmer
Apr 25, 2003
16
GR
Dear all,

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<<&quot;No &quot;<<type<<&quot; is found in the &quot;<<type<<&quot; array.\n&quot;;
	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<<&quot;No &quot;<<type<<&quot; is found in the &quot;<<type<<&quot; array.\n&quot;;
	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:
Code:
Member* get_member(Book* book);
Book* get_book(Member* member);
and the functions implementation code is:
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top