Description
It's worth noting that while the call process(shared_ptr<int>(p))
works fine in this example, it's not necessary to use anything more than just p
as the argument, because p
is already a shared_ptr
, so passing just p
creates a copy and increments the counter just fine. The reason this is important is that if p
had been defined as a built-in pointer instead, then process(shared_ptr<int>(p))
would be a problem, because the call would create a shared_ptr
pointing to the same memory that the built-in pointer p
does and set its count at 1 (not 2), so the memory pointed to by the smart pointer would be deleted when process
is finished, resulting in the built-in pointer p
becoming a dangling pointer.
Therefore, it's probably better to avoid phrasing the call in this way altogether so that we don't learn something that would be a bad habit to get into.