Changing a smart pointers object and accessing it afterwards
<p>I'm currently redesigning a rather complex program I have recently written. The program consists of various objects which depend on each other. What I'm trying to realize now is the functionality to change one of the objects and replace it with an object of the same type but different properties (which get assigned during construction of the new object for example).</p>

<p>What would be the proper way of implementing this functionality?</p>

<p><strong>What have I done so far?</strong></p>

<p>Currently I'm using a single "master class" containing a set of unique_ptr containing the different objects. Let's say object1 depends on object2, so during construction I pass the smart-pointer containing object2 by reference to the constructor of object1.<br>
As I want to be able to access object2's functions after construction, I need to somehow reference it for later use:</p>

<p>From my understanding I cannot use a raw pointer to the underlying object, as it points to a certain memory address which is likely to change when the old object gets replaced.
Currently I'm trying to use references, which reference the smart pointer itself, which should never change its address but should always be aware of where object2 is located.</p>

<p>Would this be an option? Because now I'm running into the problem that I used to have pointers to certain variables inside object2 (for easier code writing and reading mainly), however I'm pretty certain that I would run into the same address problem again. Should I use a reference to a variable inside my first reference in this case - would this even work?</p>

<p>I could also use a raw pointer referencing the smart pointer, however this feels like it isn't the right way to go. I started with C++ not too long ago, and I feel like I might have been overusing pointers, or maybe I'm not using smart pointers right, I don't know - so any help would be greatly appreciated!</p>