Hello,
I've trouble with the follwong snippet:
#include <iostream>
#include
From: "Olaf Petzold"
struct C : public BaseC { C() : b( new B("C::b") ) { } void set(const boost::shared_ptr<A>& ptr) { b.swap( boost::shared_static_cast<B>( ptr ) ); // L21 } void print() { std::cout << b->name << std::endl; } boost::shared_ptr<B> b; };
Why not simply b = boost::shared_static_cast<B>( ptr ); or b = boost::shared_polymorphic_cast<B>( ptr ); ?
Thank you,
struct C : public BaseC { C() : b( new B("C::b") ) { } void set(const boost::shared_ptr<A>& ptr) { b.swap( boost::shared_static_cast<B>( ptr ) ); // L21 } void print() { std::cout << b->name << std::endl; } boost::shared_ptr<B> b; };
Why not simply
b = boost::shared_static_cast<B>( ptr );
or
b = boost::shared_polymorphic_cast<B>( ptr );
I want to swap the pointers inside for further use (for saving the old ptr contents and restoring for circumstances - clone() may be one way before set() it but has overhead). Regards Olaf
void set(const boost::shared_ptr<A>& ptr) { b.swap( boost::shared_static_cast<B>( ptr ) ); // L21 } Why not simply
b = boost::shared_static_cast<B>( ptr ); I want to swap the pointers inside for further use (for saving the old ptr contents and restoring for circumstances
If you actually want to swap b and ptr, then shouldn't ptr be declared non-const? Tanton
From: "Olaf Petzold"
Thank you,
struct C : public BaseC { C() : b( new B("C::b") ) { } void set(const boost::shared_ptr<A>& ptr) { b.swap( boost::shared_static_cast<B>( ptr ) ); // L21 } void print() { std::cout << b->name << std::endl; } boost::shared_ptr<B> b; };
Why not simply
b = boost::shared_static_cast<B>( ptr );
or
b = boost::shared_polymorphic_cast<B>( ptr );
I want to swap the pointers inside for further use (for saving the old ptr contents and restoring for circumstances - clone() may be one way before set() it but has overhead).
You can't swap() two pointers (objects, in general) of different types, and you can't swap() with a temporary (which is what the cast returns.) One possible way is to change set() to return the old value: shared_ptr<A> set(shared_ptr<A> const & p) { shared_ptr<A> tmp(b); b = boost::shared_polymorphic_cast<B>(p); return tmp; } You can keep the old interface (minus the "const" on the parameter) if you want, but the swapping needs to be done "by hand": void set(shared_ptr<A> & p) { shared_ptr<A> tmp(b); b = boost::shared_polymorphic_cast<B>(p); p = tmp; }
Thanks for answers,
the const ptr was a fault.
Well, the following example shows the problem in detail:
----8<----
#include <iostream>
#include
From: "Olaf Petzold"
Thanks for answers,
the const ptr was a fault.
Well, the following example shows the problem in detail:
Two errors. First, Spec is not a polymorphic type - add a virtual function (destructor, for instance) to enable dynamic_cast / polymorphic_cast. Second, the exception thrown is std::bad_cast, not std::bad_alloc.
Peter Dimov wrote:
From: "Olaf Petzold"
Thanks for answers,
the const ptr was a fault.
Well, the following example shows the problem in detail:
Two errors. First, Spec is not a polymorphic type - add a virtual function (destructor, for instance) to enable dynamic_cast / polymorphic_cast. Second, the exception thrown is std::bad_cast, not std::bad_alloc.
Oh dear, it's too hot here ... It's working now - thanks a lot. Olaf
participants (3)
-
Olaf Petzold
-
Peter Dimov
-
Tanton Gibbs