Why do you think it would affect your program performance? Have you
run profiler, or it's just a guess?
I use MIC with shared_ptr a lot and it never was a bottleneck.
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
There should be no need for both pointer types. Shared_ptr handles
polymorphic objects just fine.
A MIC holding shared_ptrs to your base class should do what you need,
and the overhead associated with the shared_ptr should be negligible.
> My case is like that: I have a number of polymorphic objects which can be
> used or referenced by other objects. I put them (the polymorphic objects) in
> MIC. And if one changes its type (not only its value), its users know it
> automatically by shared_ptr. I think I need MIC with shared_ptr pointing to
> scoped_ptr. Am I right? Or is there other way to easy handle it?
How can an object change its type?! In c++ type is a compile-type attribute.
Perhaps you mean that you erase an object from the container and add
another one, or replace some object. In any case, shared_ptr would be
fine.
And what's wrong with just shared_ptr<B>? Why do you need the
additional level of indirection?
> What I mean is dynamic changing. For exampleAnd what's wrong with just shared_ptr<B>? Why do you need the
>
> class B {public: virtual ~B()} // Base class.
> class D1 : public B {} // D1 derived from B
> class D2 : public B {} // D1 derived from B
>
> scoped_ptr<B> p = new D1; // pointing to D1
> p.reset(new D2); // change pointing to D2
>
> If they are shared between owners and users, I need type
>
> shared_ptr<scoped_ptr<B>>
>
> But this type make my code hard to develop and also read.
additional level of indirection?
Ok, so you actually want the following:
1) The container itself shouldn't have "strong" references to your objects
2) An object can be recreated/replaced when it doesn't have any strong
references.
Am I right?
If so, store weak_ptr<Base> in your container:
// pseudo-code
int main()
{
multi_index_container<
weak_ptr<B>,
// some indices
> container;
shared_ptr<B> p(new D1);
container.insert(p);
// at some futher stage:
auto iterator = container.find(someKey);
auto p = iterator->lock();
// if the weak_ptr expired, i.e. no other clients use object, you
may re-create it
if (!p)
recreateIt();
}
Note, however, that there's one pitfall here: if weak_ptr expiration
affects the keys, you'll have to "refresh" MIC indices somehow.