
AFAIU, deriving from boost::noncopyable disables _accidental_ object copying. Is it possible to implement a clone() const member function of the object to _explicitly_ get a copy of the object? Perhaps deriving boost::noncopyable makes it totally impossible to ever get a copy of an object without resorting to byte copy. Regards,

2008/12/4 Hicham Mouline
AFAIU, deriving from boost::noncopyable disables _accidental_ object copying.
Is it possible to implement a clone() const member function of the object to _explicitly_ get a copy of the object?
Perhaps deriving boost::noncopyable makes it totally impossible to ever get a copy of an object without resorting to byte copy.
To avoid creating copies accidentally consider making copy constructor explicit. struct Foo { explicit Foo(const Foo&) {} }; void Bar(Foo); int main() { Foo foo; Bar(foo); // Error, implicit copy. Foo copy(foo); // OK, explicit copy. } Roman Perepelitsa.

AMDG Hicham Mouline wrote:
AFAIU, deriving from boost::noncopyable disables _accidental_ object copying.
Is it possible to implement a clone() const member function of the object to _explicitly_ get a copy of the object?
Perhaps deriving boost::noncopyable makes it totally impossible to ever get a copy of an object without resorting to byte copy.
boost::noncopyable has a private copy constructor and copy assignment operator. This prevents the compiler from generating a copy constructor and assignment operator for derived classes. That's all noncopyable does. If you want a clone function, the easiest way is probably to implement a private copy constructor and have clone call it. In Christ, Steven Watanabe

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... Regards, -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Steven Watanabe Sent: 04 December 2008 16:28 To: boost-users@lists.boost.org Subject: Re: [Boost-users] noncopyable AMDG Hicham Mouline wrote:
AFAIU, deriving from boost::noncopyable disables _accidental_ object copying.
Is it possible to implement a clone() const member function of the object to _explicitly_ get a copy of the object?
Perhaps deriving boost::noncopyable makes it totally impossible to ever get a copy of an object without resorting to byte copy.
boost::noncopyable has a private copy constructor and copy assignment operator. This prevents the compiler from generating a copy constructor and assignment operator for derived classes. That's all noncopyable does. If you want a clone function, the easiest way is probably to implement a private copy constructor and have clone call it. In Christ, Steven Watanabe

AMDG 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
auto_ptr makes more sense than a reference because the copy needs to be allocated on the heap. In Christ, Steven Watanabe

Hicham Mouline skrev:
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...
Maybe this will be of interest: http://www.boost.org/doc/libs/1_37_0/libs/ptr_container/doc/ptr_container.ht... -Thorsten

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();} };
participants (5)
-
Hicham Mouline
-
Joe Gottman
-
Roman Perepelitsa
-
Steven Watanabe
-
Thorsten Ottosen