Boost logo

Boost :

Subject: Re: [boost] How to create a shallow copy without callingaconstructor?
From: Stefan Strasser (strasser_at_[hidden])
Date: 2010-01-04 07:13:12


Am Monday 04 January 2010 10:12:03 schrieb Vicente Botet Escriba:

> Stefan Strasser-2 wrote:
> > unfortunately there is no such concept of a "deep copy" in c++ either, so
> > my
> > approach is to use Boost.Serialization for cloning objects (or a
> > user-supplied function).
> > Using serialization you can decide on a case to case basis if the object
> > behind a pointer should be copied. (depending on if the type is
> > transactional, in your case).
> > of course, that'd introduce the Serializable requirement and I don't know
> > how
> > well that fits into your design.
>
> Non , serialization don't fits my requirements as I need to use the object
> when deferred updating policy is used.

I didn't mean using the serialized stream, you can create deep copies using
Boost.Serialization: serialize the object to memory and then deserialize it.

if you keep the object that was used for serialization you have a clone, that
is a deep copy by definition because boost.serialization is designed to
reconstruct the exact same object graph that was used for serialization.

by writing your own serialization archive you could make pointers to
transactional objects shared among the clones.

>
> Stefan Strasser-2 wrote:
> > this may be different in your case though because of the requirement to
> > derive
> > from *_transaction_object, that could be used to detect which types ought
> > to
> > provide a special copy constructor.
>
> Sorry, I don't understand how the fact the type must derive from
> base_transaction_object can help to define the
> has_shallow_copy_constructor.

no, the user does have to express in some detectable way that he has a
implemented a special copy constructor (if it is optional).
this could be a type trait, or a special base class instead of
transaction_object, that can be checked for using a standard type trait.
(that's what I meant above).

or you could use inline friend functions like I do, because I wanted to avoid
a user-defined trait:

template<class T>
T *clone(T const &original){
  //default implementation
}

class user_type{
  friend user_type *clone(user_type const &original){
    //user-supplied implementation
  }
};

int main(){
  user_type t1;
  other_user_type t2;

  clone(t1); //calls user-supplied implementation
  clone(t2); //calls default implementation
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk