Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!umriscc!mcs213f.cs.umr.edu!jamesh From: jamesh@cs.umr.edu (James Hartley) Newsgroups: comp.lang.c++ Subject: solution to implementing parameterized classes Message-ID: <2792@umriscc.isc.umr.edu> Date: 8 Jun 91 15:17:12 GMT Sender: news@umriscc.isc.umr.edu Organization: University of Missouri - Rolla Lines: 169 Originator: jamesh@mcs213f.cs.umr.edu I have seen many requests for generic classes, so I am posting my solution for a parameterized queue class developed with Borland C++ 2.0 via preprocessor tricks. Stacks can likewise be implemented with minimal changes. Interested readers should refer to _Programming in C++_ by Dew- hurst & Stark (Prentice-Hall, 1989; ISBN: 0-13-723156-3) pp. 88-91 and _C++ Primer_ by Lippman (Addison-Wesley, 1989; ISBN: 0-201-16487-6) pp. 145-148. If you have any further questions, feel free to contact me by email. I will summarize to the newsgroup if sufficient interests exists. /************************ begin code ***********************************/ // FILE: main.cpp ***** #include #include #include "unqueue.h" // hides needed instantiations main() { queue(unsigned) q; // type changed to unsignedqueue by preprocessor q << 1; q << 2; q << 3; while (!q.empty()) { unsigned v; q >> v; cout << v << " "; } cout << "\n"; return 0; } // FILE: unqueue.h ***** #ifndef __UNQUEUE_H # define __UNQUEUE_H # include # include "queue.h" declare(node,unsigned); // preprocessor signal to expand macros declare(list,unsigned); declare(queue,unsigned); #endif // FILE: queue.h ***** #ifndef __QUEUE_H # define __QUEUE_H # include # include "list.h" # define queue(TYPE) _Paste2(TYPE,queue) // _Paste2 == name2 of AT&T # define queuedeclare(TYPE) \ \ class queue(TYPE) : public list(TYPE) { \ void pure() { } /* allows instantiations of class */ \ public: \ boolean operator << (TYPE &v) { return append(v); } \ boolean operator >> (TYPE &v) { return pop(v); } \ }; #endif // FILE: list.h ***** #ifndef __LIST_H # define __LIST_H # include # include "boolean.h" # define node(TYPE) _Paste2(TYPE,node) # define list(TYPE) _Paste2(TYPE,list) # define listdeclare(TYPE) \ \ class list(TYPE); /* forward reference */ \ \ class node(TYPE) { \ friend class list(TYPE); \ node(TYPE) *next; \ TYPE value; \ node(TYPE)(TYPE &v) { value = v; next = 0; } \ }; \ \ class list(TYPE) { \ node(TYPE) *head, *tail; \ protected: \ list(TYPE)(TYPE &v) { head = tail = new node(TYPE)(v); } \ list(TYPE)() { head = tail = 0; } \ virtual void pure() = 0; /* forces abstract class status */ \ boolean append(TYPE&); \ boolean push(TYPE&); \ boolean pop(TYPE&); \ public: \ boolean empty() { return boolean(head == 0); } \ }; #endif // FILE: boolean.h ***** #ifndef __BOOLEAN_H # define __BOOLEAN_H enum boolean { false, true }; #endif // FILE: unqueue.cpp ***** #include #include "unqueue.h" #include "list.cpp" implement(list,unsigned); // preprocessor signal to expand macro // FILE: list.cpp ***** #include "list.h" #define node(TYPE) _Paste2(TYPE,node) #define list(TYPE) _Paste2(TYPE,list) #define listimplement(TYPE) \ \ boolean \ list(TYPE)::append(TYPE &v) { \ node(TYPE) *p = new node(TYPE)(v); \ if (!p) /* check for unsuccessful allocation */ \ return false; \ if (!head) \ head = tail = p; /* first addition of a node to the list */ \ else \ tail = tail->next = p; /* subsequent node additions to list */ \ return true; \ } \ \ boolean \ list(TYPE)::push(TYPE &v) { \ node(TYPE) *p = new node(TYPE)(v); \ if (!p) /* check for unsuccessful allocation */ \ return false; \ if (!head) \ head = tail = p; /* first addition of a node to the list */ \ else { /* subsequent node additions to list */ \ p->next = head; \ head = p; \ } \ return true; \ } \ \ boolean \ list(TYPE)::pop(TYPE &v) { \ if (!head) /* check for empty list */ \ return false; \ v = head->value; \ node(TYPE) *p = head->next; /* retain pointer to 2nd node in list */ \ delete head; /* deallocate spent node */ \ head = p; /* reset head pointer */ \ return true; \ } /************************** end code ***********************************/ -- James J. Hartley _ /| Internet: jamesh@cs.umr.edu Department of Computer Science \'o.O' Bitnet: jamesh@cs.umr.edu@umrvmb.bitnet University of Missouri - Rolla =(___)= UUCP: ...!uunet!cs.umr.edu!jamesh "Life is like an analogy..." U ACK! PHFFT!