1

Good day, everybody!

Here is where I stuck: I have three concrete classes: Computer, Peripheral and Person. Computer and Peripheral are both extend class Device.

Device has a private member Device.owner: Person, which should store some kind of a reference to an owner of the device, and a public function Device.getOwner(): Person.

On the other hand, any Person has its own private members Person.peripherals and Person.computers and public functions Person.get() ... etc.

Is there are any design patterns to build these connections between classes?

Without transferring direct ownership! (the Computer has no rights to kill instance of Person etc. that is why I beware of using pointers)

BUT if some instances of Computer will change its owner so it must be disappeared from the list of computers of the previous owner and appeared in the list of the new owner. Same thing with Peripheral.

Please, ask any questions if you have some. Probably, my explanation of the situation is poor. Sorry for lang mistakes.

Jarod42
  • 203,559
  • 14
  • 181
  • 302

1 Answers1

1

You can use std::unique_ptr to ensure that a single reference exists to that object. To change the ownership use std::move.

std::unique_ptr<Device> devicePtr(new Device());
std::unique_ptr<Device> devicePtr2(std::move(devicePtr));

Each Person would have it's unique pointer(s).

João Ponte
  • 101
  • 1
  • 2