1

Example:

{
    std::shared_ptr<A> a(new A("First"));
    std::shared_ptr<A> b(a);
}

Which of these is the first to be deleted?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
MP0
  • 17
  • 1
  • 3
  • Did you want to know which `shared_ptr` is deleted, or when the `A` is deleted? – Mooing Duck Apr 22 '19 at 20:24
  • @MooingDuck Both. I want to know whether the object and two shared_ptr are deleted at "the same time" or if one is destroyed before the second and thus the object. – MP0 Apr 22 '19 at 20:28
  • 1
    See: https://stackoverflow.com/questions/23671945/when-is-object-pointed-by-stdshared-ptr-deleted – NathanOliver Apr 22 '19 at 20:29
  • @NathanOliver That wasn't really my question. I want know if one pointer is deleted before the other. Are both pointers existend when the object is deleted? – MP0 Apr 22 '19 at 20:43
  • 2
    When you leave the scope `b` is destroyed, then `a`, and since `a` is the last reference to the object that was created, that object is then destroyed. – NathanOliver Apr 22 '19 at 20:44
  • Possible dupe of https://stackoverflow.com/questions/10595283/order-and-point-of-calling-destructor – Mooing Duck Apr 22 '19 at 20:53
  • 1
    Execution of a function is always sequential, so you can't destroy two objects "at the same time". (If one is a subobject, its destruction will be a step of the destruction of the enclosing object.) – curiousguy Apr 22 '19 at 21:06

0 Answers0