79115318

Date: 2024-10-22 18:18:38
Score: 1
Natty:
Report link

You're mixing raw pointers and shared pointers which causes problems. To keep your Child alive until all Parents are deleted you gotta use shared pointers everywhere.

Create your Parent and Child with std::make_shared, so they are shared pointers.

When you assign the Child to a Parent, copy the shared pointer.

This way, the Child's reference count increases.

It won't be deleted until all Parents are gone.

Avoid raw pointers, they don't manage ownership.

Reasons:
  • No code block (0.5):
  • Low reputation (0.5):
Posted by: CodingCossack