Boost logo

Boost Users :

Subject: Re: [Boost-users] noncopyable
From: Joe Gottman (jgottman_at_[hidden])
Date: 2008-12-04 20:14:10


Hicham Mouline wrote:
> Like this?
>
> class C : public boost::noncopyable {
> public:
> ... clone() const
> {
> ...
> }
> private:
> C( const C& rhs )
> {
> ....
> }
> };
>
> ... instance c1 of C exists here ....
>
> What possible return types of clone() const are possible?
> .A native reference
> .An auto_ptr
>
>
> To be honest, I'm not sure myself of how it would be used...
>

   In my experience, the major reason for a clone() function is to allow
"virtual copy constructors".

struct Base
{
    virtual Base *clone() const = 0;
//You might return auto_pointer<Base> or shared_ptr<Base> instead
};

struct Derived1 : public Base
{
    Base *clone() const {return new Derived1();}
};

struct Derived2: public Base
{
     Base *clone() const {return new Derived2();}
};


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net