LearnFrenzy
Sign in Take a free test

Smart Pointers · Smart Pointers MCQ · Question 20

Converting unique_ptr to shared_ptr Answer, explanation & discussion

  • MCQ
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 20 Back to Exercise

How do you turn a unique_ptr into a shared_ptr?

  • A It is impossible
  • B Move it: std::shared_ptr<T> s = std::move(u); Correct
  • C Copy it: s = u;
  • D Call u.share()
Correct Answer: Option B

That is why factories return unique_ptr: callers can choose either kind of ownership.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links