What are one's options when the code one is working on contains a circular
dependency and one is trying to update it to use a shared_ptr vs a raw pointer?
I don't think I can easily get rid of the preexisting circular dependency. The
original author has created an inheritance where the base needs the derived.
We all know this is wrong, but it would take quite a bit to analyze how it is
used and be removed.
Am I out of luck?
Fictitious Example (best minimal example including the problems the
preexisting code has):
// File MasterClient.h
#include "BaseClient.h"
#include <set>
class MasterClient : BaseClient
{
public:
// Constructor adds this to the collection of clients
// It is later included in the processing of all clients
MasterClient();
// SNIP
private:
typedef std::set
On Fri, Mar 30, 2012 at 12:26 PM, Christopher Pisz
What are one's options when the code one is working on contains a circular dependency and one is trying to update it to use a shared_ptr vs a raw pointer?
You could use weak_ptr to break shared_ptr cycles. Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
Emil Dotchevski
On Fri, Mar 30, 2012 at 12:26 PM, Christopher Pisz
What are one's options when the code one is working on contains a circular dependency and one is trying to update it to use a shared_ptr vs a raw
austin.rr.com> wrote: pointer?
You could use weak_ptr to break shared_ptr cycles.
Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
From boost docs: Q. Can an object create a weak_ptr to itself in its constructor?
A. No. A weak_ptr can only be created from a shared_ptr, and at object construction time no shared_ptr to the object exists yet. This is the problem I cannot seem to easily overcome in both the case of the shared and weak ptr. It seems to work with a raw this pointer, but then I cannot achieve my goal.
On Fri, Mar 30, 2012 at 08:14:18PM +0000, Christopher Pisz wrote:
Emil Dotchevski
writes: You could use weak_ptr to break shared_ptr cycles.
Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
From boost docs: Q. Can an object create a weak_ptr to itself in its constructor?
A. No. A weak_ptr can only be created from a shared_ptr, and at object construction time no shared_ptr to the object exists yet.
This is the problem I cannot seem to easily overcome in both the case of the shared and weak ptr. It seems to work with a raw this pointer, but then I cannot achieve my goal.
If you have control over the places where the objects of the type are created, you might want to have two-phase initialization with a factory function to protect you from misuse. That is, something like (ignoring access and all): struct B { void init(weak_ptr<B> p) { self = p; } weak_ptr<B> self; }; struct D : B { }; shared_ptr<D> MakeD(..) { shared_ptr<D> ret = make_shared<D>(..); ret->init(ret); return ret; } -- Lars Viklund | zao@acc.umu.se
participants (3)
-
Christopher Pisz
-
Emil Dotchevski
-
Lars Viklund