Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Can I use 1 template function instead of 2?

Can I use 1 template function instead of 2?

Scheduled Pinned Locked Moved C / C++ / MFC
questiondatabasealgorithmsdata-structureslearning
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    grscot
    wrote on last edited by
    #1

    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

    P 1 Reply Last reply
    0
    • G 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.
      grscot

      P Offline
      P Offline
      Pankaj Singh
      wrote on last edited by
      #2

      try implement func^n overloading/overriding

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups