Consider the following case:
typedef boost::shared_ptr<B> BPtr; class A { public: A() { b_ptr = BPtr(new B); } void a_func() { C::c_func(b_ptr); } private: BPtr b_ptr; } class B { public: B() { b_ptr = BPtr(this); } void b_func() { C::c_func(b_ptr); } private: BPtr b_ptr; } class C { public: static void c_func(BPtr b_ptr) { /*...*/ } } Is it ok to instantiate a shared_ptr with this?
Is it ok to have two shared_ptr objects pointing to the same object? (e.g. A::b_ptr and B::b_ptr)
If one of these two gets out of scope - would B's instance be deleted?
I'm guessing I'm doing something fundamentally wrong.
I also thought about using dependency injection of b_ptr to B's constructor, but that too seems very wrong.
UPDATE:
To clarify - Both A and B need to use C::c_func. In turn, after some processing, C needs to call a callback function in B which I haven't specified above. Actually two cases are interesting:
- If there's a requirement that C won't be stateful - then it needs to receive BPtr both from A and from B as in the code above.
- If C is stateful and both A and B instantiate separate C instances, giving a BPtr in C's ctor.