a question about slicing

From: glen_stark (stark_at_ifh.ee.ethz.ch)
Date: 03/31/04


Date: Wed, 31 Mar 2004 09:00:43 -0800

Hi everyone.

I want to make sure I understand something correctly. If I have a base
and inherited classes, say shape
                                / \
                              box poly

Now let's say I have a list of pointers to shapes, and I need to create
copies of some of the shapes (which could be boxes or polys), I guess I
will have to provide a virtual copy function for box and poly, to ensure
I don't slice away any important information?

I guess what I'm asking is, if I use the default copy constructor, it
will create a copy of a shape, not of a box or a poly. Is this correct?

I'm also thinking, to prevent accidental slicing I can make shape
abstract?

Then my last question is, is there a preferred idiom for doing this?
should I just provide a member
class Shape{
virtual Shape* copy()=0;
}
for this kind of circumstance, or is there a more elegant solution?

Thanks for your time,

Glen Stark