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
 
Thomas J. Gritzan  
View profile  
 More options Feb 20 2009, 5:03 pm
Newsgroups: comp.lang.c++
From: "Thomas J. Gritzan" <phygon_antis...@gmx.de>
Date: Fri, 20 Feb 2009 18:03:36 +0100
Local: Fri, Feb 20 2009 5:03 pm
Subject: Re: Automatically create a clone() function for derived classes
Juha Nieminen schrieb:

>   I'm sure this is a frequently asked question, but...

It is (but it's not in the FAQ)!

>   Suppose that I have a hierarchy of objects which are typically
> allocated dynamically and handled with some smart pointer or whatever.
> However, I want to be able to create copies of those objects, while
> having only a pointer-to-base-class. The solution is to write a clone()
> member function like this:

> 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:

[...]

1) You can use CRTP to avoid writing all the code again and again.
2) You can try some programming by contract to check if someone forgot
to implement its clone().

Both is described in this posting (the complete thread might be of
interest, too):
http://groups.google.de/group/comp.lang.c++.moderated/msg/993079dca61...

--
Thomas


    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