Hi, so I have a simple question in terms of dependency injection with c++. Say I have 3 classes A, B and C. If both B and C have a dependency on A, what is the best way to inject and share A with both B and C. I could have the following (pseudo code):
B(A& a) : uniqueAptr(&a){}
C(A& a) : uniqueAptr(&a){}
Where uniqueAptr is a unique_ptr (std::unique_ptr uniqueAptr) member variable and then intialize B and C like so:
A aObject;
B bObject(aObject);
C cObject(aObject);
But wouldn't that cause both bObject and cObject object to delete the pointer to aObject effectively trying to delete aObject twice (which would cause an error). Would a shared_ptr make most sense in this case? I've read a few resources online saying that shared_ptr is not all that great to use for exampple http://programmers.stackexchange.com/questions/133302/stdshared-ptr-as-a-last-resort . So what's the preferred way of doing something like the above? This is a simple example, but in reality you would have other pointer member variable and so on.