Click here to Skip to main content
15,910,411 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: STL algorithm Pin
Rage30-Apr-03 5:42
professionalRage30-Apr-03 5:42 
GeneralRe: STL algorithm Pin
User 988530-Apr-03 2:18
User 988530-Apr-03 2:18 
GeneralRe: STL algorithm Pin
Rage30-Apr-03 5:42
professionalRage30-Apr-03 5:42 
GeneralRe: STL algorithm Pin
Giles30-Apr-03 2:35
Giles30-Apr-03 2:35 
GeneralRe: STL algorithm Pin
Rage30-Apr-03 5:41
professionalRage30-Apr-03 5:41 
GeneralRe: STL algorithm Pin
Andrew Walker30-Apr-03 2:55
Andrew Walker30-Apr-03 2:55 
GeneralRe: STL algorithm Pin
Rage30-Apr-03 5:40
professionalRage30-Apr-03 5:40 
QuestionCan I use 1 template function instead of 2? Pin
grscot30-Apr-03 1:28
grscot30-Apr-03 1:28 
Dear all,
I have two template functions for displaying books and members respectively from arrays.

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


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


The function prototypes are:

<br />
Member* get_member(Book* book);<br />
Book* get_book(Member* member);<br />


and the functions implementation code is:


<br />
template<class Book,class Member><br />
Member* AssociationList<Book,Member>::get_member(Book* book)<br />
{<br />
	Member* member=0;<br />
	bool searching=true;<br />
	int index=0;<br />
<br />
	while(searching)<br />
	{<br />
		if (this->association_list[index])<br />
			if (this->association_list[index]->linked_book()==book)<br />
			{<br />
				member=this->association_list[index]->linked_member();<br />
				searching=false;<br />
			}<br />
			else<br />
				index++;<br />
		else<br />
			index++;<br />
		if (searching && (index == LIST_SIZE))<br />
		{<br />
			searching=false;<br />
		}<br />
	}<br />
	return member;<br />
}<br />


<br />
template<class Book,class Member><br />
Book* AssociationList<Book,Member>::get_book(Member* member)<br />
{<br />
	Book* book=0;<br />
	bool searching=true;<br />
	int index=0;<br />
<br />
	while(searching)<br />
	{<br />
		if (this->association_list[index])<br />
			if (this->association_list[index]->linked_member()==member)<br />
			{<br />
				book=this->association_list[index]->linked_book();<br />
				searching=false;<br />
			}<br />
			else<br />
				index++;<br />
		else<br />
			index++;<br />
		if (searching && (index == LIST_SIZE))<br />
		{<br />
			searching = false;<br />
		}<br />
	}<br />
	return book;<br />
}<br />

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

AnswerRe: Can I use 1 template function instead of 2? Pin
Pankaj Singh30-Apr-03 3:42
Pankaj Singh30-Apr-03 3:42 
GeneralEditor Pin
y_seval30-Apr-03 1:06
y_seval30-Apr-03 1:06 
GeneralRe: Editor Pin
Neville Franks30-Apr-03 1:40
Neville Franks30-Apr-03 1:40 
GeneralDateTimePicker problem! Pin
_skidrow_vn_30-Apr-03 1:03
_skidrow_vn_30-Apr-03 1:03 
GeneralRe: DateTimePicker problem! Pin
David Crow30-Apr-03 3:01
David Crow30-Apr-03 3:01 
GeneralRe: DateTimePicker problem! Pin
_skidrow_vn_30-Apr-03 7:49
_skidrow_vn_30-Apr-03 7:49 
GeneralTwo Rich Edit Questions Pin
Gio S.29-Apr-03 23:03
Gio S.29-Apr-03 23:03 
GeneralRe: Two Rich Edit Questions Pin
John R. Shaw30-Apr-03 9:27
John R. Shaw30-Apr-03 9:27 
GeneralCRichEdit & smtp mail problem Pin
granburu29-Apr-03 22:03
granburu29-Apr-03 22:03 
GeneralWindow handle of a process-code required Pin
RaajaOfSelf29-Apr-03 21:59
RaajaOfSelf29-Apr-03 21:59 
GeneralRe: Window handle of a process-code required Pin
Rage30-Apr-03 0:51
professionalRage30-Apr-03 0:51 
GeneralMiniDumpReadDumpStream Pin
Riverwind_dk29-Apr-03 21:58
Riverwind_dk29-Apr-03 21:58 
Generalwinpcap has killed me Pin
summo29-Apr-03 21:27
summo29-Apr-03 21:27 
GeneralRe: winpcap has killed me Pin
jhaga30-Apr-03 1:33
professionaljhaga30-Apr-03 1:33 
GeneralCListBox Pin
Cedric Moonen29-Apr-03 21:10
Cedric Moonen29-Apr-03 21:10 
GeneralRe: CListBox Pin
Martyn Pearson29-Apr-03 21:41
Martyn Pearson29-Apr-03 21:41 
GeneralInstall Driver Pin
peterchen29-Apr-03 20:49
peterchen29-Apr-03 20:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.