Boost logo

Boost Users :

From: Khandelwal, Amit (amit.khandelwal_at_[hidden])
Date: 2008-06-25 07:49:14


I read the discussion and tried it using gcc version 3.2.3 I am getting the following error.

clone2.cpp:34: sorry, not implemented: adjusting pointers for covariant returns
clone2.cpp:34: sorry, not implemented: adjusting pointers for covariant returns
clone2.cpp:34: sorry, not implemented: adjusting pointers for covariant returns
clone2.cpp: In member function `std::auto_ptr<Derived>
   Cloneable<Derived>::clone() const [with Derived = A]':
clone2.cpp:50: instantiated from here

clone2.cpp:17: invalid static_cast from type `CloneableBase*' to type `A*

I think my code is right. Any thoughts?

#include <memory>

class CloneableBase {
public:
        virtual ~CloneableBase() {};

protected:
        virtual CloneableBase* doClone() const = 0 ;
};

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

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

        virtual CloneableBase* doClone() const {
            return new Derived( static_cast< const Derived& >(*this) );
        }
};

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();
    }
}

-----Original Message-----
From: boost-users-bounces_at_[hidden] [mailto:boost-users-bounces_at_[hidden]] On Behalf Of Daniel Krügler
Sent: Wednesday, June 25, 2008 6:00 AM
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] shared_ptr invalid covariant return type

John wrote:
> Thorsten Ottosen wrote:
>
>> John skrev:
>>
>>> This pattern/idiom is well-established:
>>>
>>> struct A {
>>> virtual A* clone () = 0;
>>> };
>>>
>>> struct B : public A {
>>> virtual B* clone ();
>>> };
>>>
>>>
>>> But the above does not work when the pointers A* and B* are replaced
>>> by shared_ptr<A> and shared_ptr<B>:
>>>
>>> struct A {
>>> virtual shared_ptr<A> clone () = 0;
>>> };
>>>
>>> struct B : public A {
>>> virtual shared_ptr<B> clone ();
>>> };
>>>
>>>
>>> Is there a workaround? Another way to solve the same problem? Thanks!
>>
>>
>> Well, don't use shared_ptr here. A newly created clone is not shared,
>> and it makes the interface less usable in other context to return a
>> shared ptr here.
>>
>> -Thorsten
>
>
>
> Hah! Well, yes, I guess it's true that there's always an option not to
> use smart pointers at all. :) Perhaps people will make too many
> assumptions about the method based on the name, "clone". Instead,
> pretend the clone() method is named something else, without any other
> assumptions.
>
> For example, pretend it's a get_row() method for a database cursor
> abstraction. The method might trigger a fetch, in which case the
> returned object is not shared, or it might return a previously fecthed
> row. Also, whether this method is usable in non-smart-pointer context
> is not a concern. -John

If you are interested in a more generalized approach of the same problem, you should have alook at

http://preview.tinyurl.com/3w6elg

Greetings from Bremen,

Daniel Krügler

_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

This message is intended only for the personal and confidential use of the designated recipient(s) named above. If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited. This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers. Email transmission cannot be guaranteed to be secure or error-free. Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such. All information is subject to change without notice.

--------
IRS Circular 230 Disclosure:
Please be advised that any discussion of U.S. tax matters contained within this communication (including any attachments) is not intended or written to be used and cannot be used for the purpose of (i) avoiding U.S. tax related penalties or (ii) promoting, marketing or recommending to another party any transaction or matter addressed herein.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net