Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion Automatically create a clone() function for derived classes
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
blargg  
View profile  
 More options Feb 22 2009, 9:10 pm
Newsgroups: comp.lang.c++
From: blargg....@gishpuppy.com (blargg)
Date: Sun, 22 Feb 2009 15:10:14 -0600
Local: Sun, Feb 22 2009 9:10 pm
Subject: Re: Automatically create a clone() function for derived classes
In article <l3znl.97$zj6...@read4.inet.fi>, Juha Nieminen
<nos...@thanks.invalid> wrote:

[...]
> class Base
> {
>  public:
>     virtual Base* clone() const { return new Base(*this); }

>     // other stuff here
> };

>   The problem is that this clone() function must be replicated in all
> the derived classes as well, and their implementation is always the
> same, except for the type which is being allocated. For example:

> class Derived: public Base
> {
>  public:
>     virtual Base* clone() const { return new Derived(*this); }
> };

>   It's not only tedious to have to write this line in each of the
> derived classes, but it's also error-prone: If you ever forget to add
> that line to some derived class, no compiler error will happen, and the
> program will even run, but when cloning a derived object using a base
> type pointer, you get a crippled copy, so the whole thing malfunctions,
> and it might malfunction in a very non-obvious way which is hard to
> track and debug.

>   Isn't there any way of automating this process?

[...]

Odd-ball approach that comes to mind, if compile-time checking is
insisted on:

    class Clonable {
    protected:
        template<class T>
        Clonable( T const* t )
        {
            // ensure that T implements covariant clone
            if ( false )
                t = t->clone();
        }

        // OK to remove this
        virtual Clonable* clone() const = 0;
    };

    class Base : protected virtual Clonable {
    public:
        Base() : Clonable( this ) { }
        Base* clone() const { return new Base( *this ); }
    };

    class Derived : public Base {
    public:
        Derived() : Clonable( this ) { }
        Derived* clone() const { return new Derived( *this ); }
    };

    class Error : public Derived {
    public:
        // error, Clonable doesn't have default ctor
        Error() /* : Clonable() */ { }
    };

    class Error2 : public Derived {
    public:
        // error, clone() doesn't return Error2*
        Error2() : Clonable( this ) { }
    };

You could probably use a similar approach, with Clonable generating the
clone function automatically, and the derived class just needing to pass
'this' to Clonable so that its template will be instantiated for the
derived type.


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2010 Google