Talk About Network



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 > C++ language: C...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 16 Topic 9571 of 9593
Post > Topic >>

C++ language: Cloneable classes

by =?ISO-8859-2?Q?Krzysiek_Czai=F1ski_=22Czajnik=22?= <1czajnik@[EMAIL PROTECTED] May 5, 2008 at 07:10 AM

I'm working on a Cloneable concept for C++ classes. Here's what I
think it should be like, however it doesn't compile :?

#include <memory>

class CloneableBase
{
public:
    virtual ~CloneableBase() {}
private:
    virtual CloneableBase* doClone() const = 0;
};

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

class A : public Cloneable<A>
{
    virtual A* doClone() { return new A( *this ); }
};

int main()
{
    {
       A a;
       A::AutoPtr pa = a.clone();
    }
}

cloneable.cpp:17: error: invalid covariant return type for 'Derived*
Cloneable<Derived>::doClone() const [with Derived = A]'
gcc version 4.2.3 (Gentoo 4.2.3 p1.0)

MSVC.NET 2005 gives a similar error on this..

The C++ standard says (10.3.5)
The return type of an overriding function shall be either identical to
the return type of the
overridden function or covariant with the classes of the functions.

Since Derived = A, it is derived from CloneableBase, through
Cloneable<A>, so Derived is covariant with CloneableBase.

Is my understanding wrong, or is this the compilers' bug, or maybe
it's just the way it is supposed to be, and I misunderstand the C++
standard? I'm also looking forward to comments on design/style.

Regards
Krzysztof CzaiƄski


-- 
      [ 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 Wed May 14 11:01:13 CDT 2008.