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 14 of 16 Topic 9571 of 9775
Post > Topic >>

Re: C++ language: Cloneable classes

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

On May 9, 5:15 pm, Daniel Krügler <daniel.krueg...@[EMAIL PROTECTED]
>
wrote:

> I still think that it would be fine to add:
> protected:
>       virtual Cloneable* doClone() const = 0;

In this small example main.cpp:
[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 new C; } };
int main()
{
	B b;
	C c;
	b.clone();
	c.clone();
	b.clone< boost::shared_ptr<A> >(); // see code below
	c.clone< boost::shared_ptr<A> >();
}
[/code]

g++ main.cpp
main.cpp:12: error: cannot declare variable ‘b’ to be of abstract type
‘B’
main.cpp:6: note:   because the following virtual functions are pure
within ‘B’:
cloneable.h:63: note: 	Cloneable<Derived, DefaultPtrPolicy>*
Cloneable<Derived, DefaultPtrPolicy>::doClone() const [with Derived =
A, DefaultPtrPolicy = std::auto_ptr<A>]

Adding this breaks compatibility with NaturallyCloneable<> -- class B
from the exmaple main.cpp becomes abstract, and code doesn't compile.

> An interesting enhancement proposal for Cloneable could be to
> add one further template parameter as smart-pointer policy

> Yes, this is fine. And by means of the virtual inheritance you
> simulate what other languages usually call "interface" (but
> you know that). And if you use Cloneable<D, boost::shared_ptr<D> >
> this will match the style of those languages even more ;-)

Yes, exactly ;-)

Here's the enhancement You proposed with another little enhancement of
my own:

[code]
template < typename Derived, typename DefaultPtrPolicy =
std::auto_ptr<Derived> >
class Cloneable : public virtual CloneableBase
{
public:

     BOOST_STATIC_ASSERT(( boost::is_base_of< Derived, typename
DefaultPtrPolicy::element_type >::value ));

     typedef DefaultPtrPolicy SmartPtr;

     SmartPtr clone() const { return clone<SmartPtr>(); }

     template < typename OtherPtrPolicy >
     OtherPtrPolicy clone() const
     {
         BOOST_STATIC_ASSERT(( boost::is_base_of< Derived, typename
OtherPtrPolicy::element_type >::value ));
         // note: must be dynamic_cast due to virtual inheritance of
CloneableBase
         Derived* p = dynamic_cast<Derived*>( doClone() );
         BOOST_ASSERT( p != NULL );
         return OtherPtrPolicy( p );
     }


// note: the below would break compatibility with NaturallyCloneable
//protected:
//    Cloneable* doClone() const = 0;
};
[/code]

Still, one thing bothers me.. The static assertion "is_base_of" should
stay, but it would be nice to also be able to use a normal Derived*
instead of some smart ptr type. Can that be achieved simply?

class X : public Cloneable<X,X*>, NaturallyCloneable<X> {};


-- 
      [ 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:37:13 CDT 2008.