Path: utzoo!mnetor!uunet!husc6!mit-eddie!uw-beaver!ubc-vision!ubc-cs!ubc-csgrads!coatta
From: coatta@ubc-csgrads.uucp (Terry Coatta)
Newsgroups: comp.lang.c++
Subject: Virtual Functions
Message-ID: <1764@ubc-cs.UUCP>
Date: 8 Dec 87 20:13:06 GMT
Sender: nobody@ubc-cs.UUCP
Lines: 40


I have been working on a set of generic list routines (as a way of
learning c++).  The method I have chosen to use is to supply a
class Link, from which the user can derive (publicly) his own
classes.  Since the class List functions use parameters of
type Link& for the most part, the user can simply pass items
of his own class to them.

The problem that I have encountered is that within the the List
class I want to provide a function which copies a list.  I originally
thought I could do this by:

     .
     .
     newlink = new Link(current);
     newlistprev->next = newlink;
     .
     .

that is, I would scan down the current list using the new operator
to duplicate nodes for me (via the Link::Link(Link&) constructor).
Then I realized that this would not work since that constructor
would not know how to copy the user's data.  It seems like the
Link class has to have a virtual function Duplicate() which
duplicates an instance.  The user derived class would have to
contain a Duplicate() function which would duplicate its own
data appropriately, and then call Link::Duplicate to ensure that
its duplication was handled correctly.

Is this more or less correct?  Does anybody have other suggestions?
I realize that there are other methods of approaching the
generic list problem, but this is the one I would like to use.

Terry Coatta
Dept. of Computer Science, UBC, Vancouver BC, Canada
coatta@grads.cs.ubc.cdn

`What I lack in intelligence, I more than compensate for with stupidity'