Topic: standard 'clone' method? Other possibilites


Author: deef@teleport.com (Derek Foster)
Date: 7 Feb 1995 21:02:01 -0800
Raw View
In <MATT.95Feb1223956@physics2.berkeley.edu> matt@physics2.berkeley.edu (Matt Austern) writes:
>In article <russgold-0102952353040001@slip-19.netaxs.com> russgold@netaxs.com (Russell Gold) writes:

>> My apologies if this has already been suggested, but I would like to see
>> some kind of standard 'clone' function for all classes:

>The reason it won't happen is that this functionality would only be
>possible for polymorphic classes---i.e., classes that have dynamic
>type information.

All right, how about restricting cloning to only polymorphic classes,
and trying something like:

1) creating a dynamic_copy<BaseClass>(x) operator that
   would use RTTI (if available for class BaseClass, else a compiler error)
   to return the results of 'new DerivedClass(x)' for the given x?

or,
2) Requiring that any base class which is declared as:

class clonable X
{
}

have, effectively, a virtual 'clone' method added to all derived classes,
which for each class performed the equivalent of 'new Derived(*this)'.

or,
3) Extending the 'typeid' operator such that, for each TypeInfo object
   returned by typeid(x), there effectively exists a member function,
   'dynamic_copy(const x & param)' which can successfully copy any derived
   class of x via 'new' and the copy constructor. Thus, to copy an object,
   one would write:
       Thing * x = typeid(x).dynamic_copy(x);


My problem with the proposals given which use macros is that they require
every derived class to contain a macro invocation, which causes disaster
if it is omitted.

Derek Riippa Foster


--
deef@teleport.com  Public Access User --- Not affiliated with TECHbooks
Public Access UNIX and Internet at (503) 220-1016 (2400-14400, N81)




Author: jason@cygnus.com (Jason Merrill)
Date: Wed, 8 Feb 1995 12:05:58 GMT
Raw View
>>>>> Derek Foster <deef@teleport.com> writes:

Hi, Derek!

> 1) creating a dynamic_copy<BaseClass>(x) operator that
>    would use RTTI (if available for class BaseClass, else a compiler error)
>    to return the results of 'new DerivedClass(x)' for the given x?

This one makes the most sense to me, though I don't see the need for the
<BaseClass> syntax; the type of x should give you all the information you
need to start looking.

Does someone want to write the code to add this to g++?  :)

Jason




Author: kevlin@wslint.demon.co.uk (Kevlin Henney)
Date: Thu, 9 Feb 1995 09:03:08 +0000
Raw View
In article <3h9j8a$spt@linda.teleport.com>
           deef@teleport.com "Derek Foster" writes:

>In <MATT.95Feb1223956@physics2.berkeley.edu> matt@physics2.berkeley.edu (Matt
> Austern) writes:
>>In article <russgold-0102952353040001@slip-19.netaxs.com> russgold@netaxs.com
> (Russell Gold) writes:
>
>>> My apologies if this has already been suggested, but I would like to see
>>> some kind of standard 'clone' function for all classes:
>
>>The reason it won't happen is that this functionality would only be
>>possible for polymorphic classes---i.e., classes that have dynamic
>>type information.
>
>All right, how about restricting cloning to only polymorphic classes,
>and trying something like:
>
>1) creating a dynamic_copy<BaseClass>(x) operator that
>   would use RTTI (if available for class BaseClass, else a compiler error)
>   to return the results of 'new DerivedClass(x)' for the given x?

What about classes whose objects are not copyable? Notionally, there are
quite a lot of these, but sometimes programmers don't realise that they should
declare the cp ctor and assignment op private - instead they leave the
compiler to generate dangerous copy operations <shudder>.

What would dynamic_copy - or indeed the other posted suggestions - do in
these cases? Throw an exception? <shudder some more>

+---------------------------+------------------------------------+
| Kevlin A P Henney         | Money confers neither intelligence |
| kevlin@wslint.demon.co.uk | nor wisdom on those who spend it   |
| Westinghouse Systems Ltd  |                                    |
+---------------------------+------------------------------------+