[comp.lang.c++] Tricky question answered

ngo@tammy.harvard.edu (Tom Ngo) (09/26/90)

Earlier today, I wrote:

ngo> I would appreciate any advice about the following situation.
ngo> [...]
ngo> This means that inside Collector::memorize() I must invoke "new B"...
ngo> but that is obviously not going to be enough, since a B cannot hold
ngo> all of the information in, say, a B1.  Are there any suggestions on
ngo> how to handle this?

I have thought of an answer that satisfies me:  class B should contain
a virtual function called clone() which, when invoked with any of the
derived classes, invokes new in a class-specific manner:

    B1* B1::clone(void) const
    {
	return new B1(class_specific_arguments);
    }

So my method Collector::memorize(B *p) can do

    B *q = p->clone();

to get durable copies of the objects to which p points.

Sorry to waste bandwidth while thinking aloud.  If I still get replies
and they are different from this idea, I might still post a summary.

--
  Tom Ngo
  ngo@harvard.harvard.edu
  617/495-1768 lab number, leave message