home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!usc!sdd.hp.com!ux1.cso.uiuc.edu!m.cs.uiuc.edu!sunb10.cs.uiuc.edu!sparc6.cs.uiuc.edu!pjl
- From: pjl@sparc6.cs.uiuc.edu (Paul Lucas)
- Subject: Re: Generic types
- Message-ID: <1992Aug29.163433.10007@sunb10.cs.uiuc.edu>
- Sender: news@sunb10.cs.uiuc.edu
- Organization: University of Illinois at Urbana-Champaign
- References: <l9scgqINNh7u@exodus.Eng.Sun.COM>
- Date: Sat, 29 Aug 1992 16:34:33 GMT
- Lines: 74
-
- In <l9scgqINNh7u@exodus.Eng.Sun.COM> obourdon@France.Sun.COM (Olivier Bourdon - Sun ICNC) writes:
-
- >Let's assume I have two different types of objects (classes)
- >//-------------------
- >class complex {
- >public:
- > int i,j;
- > ...
- > void Print();
- >};
- >//--------------------
- >class string
- >public:
- > char *str;
- > ...
- > void Print();
- >};
-
- >and I also have a double linked list
- >//-----------------
- >class list {
- > void *element;
- > list *next, *prev;
- > void Print();
- >};
-
- >then I woul like to do something like :
- >void list::Print() {
- > while (this->next!=NULL)
- > this->elem->Print();
- > ...
- >}
-
- >I know that it does not work AT COMPILE TIME because the Print function is not
- > a member function for void * type. It seems like I could create sur-classes
- > of list by removing the element field in the list class and adding :
- >class stringlist {
- > string *element ;
- > ...
- >};
-
- >but then I have to write 2 times the EXACTLY SAME CODE once for
- >stringlist::Print and the other one for complexlist::Print
- >and I would like to have only one piece of code so that if I have to
- >modify it I do it only at one place
-
- *****> You have to have a base Link class that has Print() in it from which
- all other things that are to be on the list have to be derived from.
- (This make for what is called an "intrusive list.") Print() has to
- be virtual.
-
- You can also decide whether you want the list to be "managed" or
- not, i.e., when the list goes away, does it destroy the elements
- also. Stroustrup, 2nd ed., has examples of this very thing. I've
- done things similar to:
-
- class Link { // not List since it's just a link in a list
- Link *prev, *next;
- Link *element;
- public:
- // ...
- virtual void Print() = 0;
- };
-
- class Foo : public Link {
- // ...
- public:
- // ...
- void Print();
- };
- --
- - Paul J. Lucas University of Illinois
- AT&T Bell Laboratories at Urbana-Champaign
- Naperville, IL pjl@cs.uiuc.edu
-