boaschtel
boaschtel

Reputation: 3

Changing a smart pointers object and accessing it afterwards

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).

What would be the proper way of implementing this functionality?

What have I done so far?

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.
As I want to be able to access object2's functions after construction, I need to somehow reference it for later use:

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.

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?

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!

Upvotes: 0

Views: 2673

Answers (1)

Jack
Jack

Reputation: 133587

What you need to do, assuming that ownership is unique (so no shared_ptr) so that your MasterClass and only MasterClass owns all instances of sub-objects, is to use unique_ptr and pass them around by const l-value reference so that you will be able to access always the correct address of a specific sub-object (since unique_ptr also works as a wrapper).

What I mean is something like:

class Object2 {
  const std::unique_ptr<Object1>& object1;
public:
  Object2(const std::unique_ptr<Object1>& object1) : object1(object1) { }
}

class MasterClass {
  std::unique_ptr<Object1> object1;
  std::unique_ptr<Object2> object2;

  MasterClass() : object1(new Object1()), object2(new Object2(object1)) { }

  void replace() {
    // no problem for Object2
    object1.reset(new Object1());
  }
}

Mind that you could use non-const references if you need to change the managed object from a class which is not MasterClass but this exposes some problems since a std::unique_ptr passed by non const reference does't really assure you that the called function won't take ownership by moving into its own unique_ptr or such subtle things.

Upvotes: 1

Related Questions