Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: C++ languag...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 12 of 16 Topic 9571 of 9775
Post > Topic >>

Re: C++ language: Cloneable classes

by Krzysztof Czainski <1czajnik@[EMAIL PROTECTED] > May 8, 2008 at 09:31 PM

My final approach:

[code]
/** Cloneable interface for classes.
  * @[EMAIL PROTECTED]
 Krzysztof Czainski
  * @[EMAIL PROTECTED]
 2008.05.04
  */

#pragma once
#include <memory>
#include <boost/assert.hpp>
#include <boost/cast.hpp>

/////////////////////////////////////////////////////////////////////////////

/** Inherit virtually. Base class for Cloneable<> and
NaturallyCloneable<> */
class CloneableBase
{
public:

	/** @[EMAIL PROTECTED]
 AC- */
	virtual ~CloneableBase() {};

protected:

	/** @[EMAIL PROTECTED]
 new copy of *this
	 * @[EMAIL PROTECTED]
 aCI */
	virtual CloneableBase* doClone() const = 0 ;

};

/////////////////////////////////////////////////////////////////////////////

/** class User : public Cloneable<User> */
template < typename Derived >
class Cloneable : public virtual CloneableBase
{
public:

	typedef std::auto_ptr<Derived> AutoPtr;

	/** @[EMAIL PROTECTED]
 aCI */
	AutoPtr clone() const
	{
		return
AutoPtr( boost::polymorphic_downcast<Derived*>( doClone() ) );
	}

};

/////////////////////////////////////////////////////////////////////////////

/** class UserFinal : public NaturallyCloneable<UserFinal> */
template < typename Derived >
class NaturallyCloneable : public virtual CloneableBase
{
protected:

	/** @[EMAIL PROTECTED]
 CloneableBase
	 * @[EMAIL PROTECTED]
 aCI */
	virtual CloneableBase* doClone() const
	{
		// prevent slicing and assert corectnes of static_cast
		BOOST_ASSERT( typeid(*this) == typeid(Derived) );
		return new Derived( static_cast< const Derived& >(*this) );
	}

};
[/code]

I won't use the improved version of Daniel Krügler's code, because in
the example below class NaturallyCloneable<B> wouldn't provide
implementation for Cloneable<A>::doClone, which would make it
abstract.

Example:
[code]
class A : public Cloneable<A> {}; // abstract base for B and C
class B : public A, public NaturallyCloneable<B> {};
class C : public A { virtual A* doClone() const { /* return hand-made
copy of *this */ } };
[/code]

As for Dizzy's suggestion of using boost::variant, and compile-time
polymorphism: It seems like a very interesting design, which I would
have never thought about. However, I won't use it, because my design
takes advantage of runtime polymorphism any way.


-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 16 Posts in Topic:
C++ language: Cloneable classes
=?ISO-8859-2?Q?Krzysiek_C  2008-05-05 07:10:17 
Re: C++ language: Cloneable classes
Marsh Ray <marsh527@[E  2008-05-05 12:34:54 
Re: C++ language: Cloneable classes
Mathias Gaunard <loufo  2008-05-06 08:21:03 
Re: C++ language: Cloneable classes
=?ISO-8859-2?Q?Krzysiek_C  2008-05-06 08:43:26 
Re: C++ language: Cloneable classes
Krzysztof Czainski <1c  2008-05-06 12:46:54 
Re: C++ language: Cloneable classes
Mathias Gaunard <loufo  2008-05-06 18:43:26 
Re: C++ language: Cloneable classes
=?ISO-8859-1?Q?Daniel_Kr=  2008-05-06 18:43:26 
Re: C++ language: Cloneable classes
Krzysztof Czainski <1c  2008-05-07 11:08:38 
Re: C++ language: Cloneable classes
Krzysztof Czainski <1c  2008-05-07 11:43:27 
Re: C++ language: Cloneable classes
dizzy <dizzy@[EMAIL PR  2008-05-07 11:43:28 
Re: C++ language: Cloneable classes
=?ISO-8859-1?Q?Daniel_Kr=  2008-05-07 18:29:53 
Re: C++ language: Cloneable classes
Krzysztof Czainski <1c  2008-05-08 21:31:47 
Re: C++ language: Cloneable classes
=?ISO-8859-1?Q?Daniel_Kr=  2008-05-09 09:15:41 
Re: C++ language: Cloneable classes
Krzysztof Czainski <1c  2008-05-09 21:26:23 
Re: C++ language: Cloneable classes
=?ISO-8859-1?Q?Daniel_Kr=  2008-05-10 14:13:29 
Re: C++ language: Cloneable classes
Krzysztof Czainski <1c  2008-05-11 16:43:29 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Jul 8 23:44:46 CDT 2008.