18.4k views
1 vote
Which smart pointer should be used for exclusive ownership of the newly created type (new a)?

a) std exclusive_ptr p(new a);
b) std auto_ptr p(new a());
c) std pointer p(new a);
d) std unique_ptr p(new a);

1 Answer

7 votes

Final answer:

The smart pointer for exclusive ownership in C++ is std::unique_ptr, as it ensures only one instance owns the resource preventing any copy or assignment of the pointer.

Step-by-step explanation:

The smart pointer that should be used for exclusive ownership of the newly created type (new a) is std::unique_ptr. The correct option is d) std::unique_ptr<a> p(new a);.

This smart pointer ensures that only one pointer can own a particular resource at a time. It provides a strict ownership policy that does not allow copying or assigning the ownership of the pointer, which is essential to avoid resource duplication and leaks.

User Mohammad Nikravesh
by
8.6k points