Boost logo

Boost :

Subject: Re: [boost] How to create a shallow copy without calling a constructor?
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2010-01-02 14:03:01


vicente.botet wrote:

> I have tried with
>
> class C {
> public:
> C* shallow_clone() {
> C* p = reinterpret_cast<C>(malloc(sizeof(C));
> if (p==0) {
> throw std::bad_alloc();
> }
> memcpy(p, this, sizeof(C));
> return p;
> }
> };

Or, maybe more appropriate,

C* shallow_clone() {
   C* p = static_cast<C*>(::operator new(sizeof(C)));
   memcpy(p, this, sizeof(C));
   return p;
}


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