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

Re: C++ language: Cloneable classes

by =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@[EMAIL PROTECTED] May 7, 2008 at 06:29 PM

On 7 Mai, 19:43, Krzysztof Czainski <1czaj...@[EMAIL PROTECTED]
> wrote:
> On 7 Maj, 02:43, Daniel Krügler <daniel.krueg...@[EMAIL PROTECTED]
>
> wrote:
> > "If the return type of D::f differs from the return type of
> > B::f, the class type in the return type of D::f shall be
> > complete at the point of declaration of D::f or shall be the
> > class type D."
>
> > In your example, Derived is incomplete at this point and
> > different from Cloneable.
>
> I see. This explains everything, thanks. So I need a workaround..
>
> > #include <memory>
>
> > class CloneableBase
> > {
> > public:
> >      virtual ~CloneableBase() {}
> > protected:
> >      virtual CloneableBase* doClone() const = 0;
>
> > };
>
> > template < typename Derived>
> > class Cloneable : public CloneableBase
> > {
> > public:
> >      typedef std::auto_ptr<Derived> AutoPtr;
> >      AutoPtr clone() const {
> >        return AutoPtr( static_cast<Derived*>(this->doClone()) );
> >      }
>
> > };
>
> Yes, I implemented a similar solution in my code, but until now, I
> considered it a tem****ary workaround.. I now see, that it's the final
> solution, thanks.

IMO my proposed code could be slightly improved
to come even nearer to your original trial. Essentially
the fix needs only to be done in *Cloneable*, were we
take advantage of the maximum which the standard
allows, i.e. we reintroduce the private doClone() in
the CloneableBase and in Cloneable, whereby we use
Cloneable as the most derived type. The static_cast
remains, but we prevent a subset of previously
allowed misusages:

template < typename Derived >
class Cloneable : public CloneableBase
{
public:
     typedef std::auto_ptr<Derived> AutoPtr;
     AutoPtr clone() const { return AutoPtr(
  static_cast<Derived*>(doClone()) ); }
private:
     virtual Cloneable* doClone() const = 0;
};

Greetings from Bremen,

Daniel Krügler


-- 
      [ 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:39:52 CDT 2008.