Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8734309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:49:06+00:00 2026-06-13T09:49:06+00:00

What is the best way of returning a pointer from a factory ? Should

  • 0

What is the best way of returning a pointer from a factory ? Should it be a std::unique_ptr or std::shared_ptr or just a raw pointer?

Also, I was told, that, one should go for std::unique_ptr if there is containment and std::shared_ptr if there is aggregation. Is this the correct way ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:49:07+00:00Added an answer on June 13, 2026 at 9:49 am

    You should be considering raw pointers only in very special cases, such as passing the pointer across a DLL boundary.

    Between shared_ptr and unique_ptr, my opinion is to prefer the latter. This leaves the interface more flexible for your users. They can always convert the returned unique_ptr to a shared_ptr if they desire to, but more importantly, they can also call unique_ptr::release and then manually manage the pointer (maybe not a good idea, but it leaves the option open).

    If your factory needs to assign a custom deleter for the returned unique_ptr, a difference in behavior between unique_ptr and shared_ptr you should be aware of is that the former will not call the deleter if the managed pointer is nullptr, but the latter will. So, if your factory may return nullptr (maybe as a failure condition), and someone converts the unique_ptr to a shared_ptr, then make sure the deleter can handle being called with nullptr as its argument.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way (performance-wise) of returning stl containers from a function? The
I just wonder what's the best way to apply a function that returns Void
What is the best practice when returning a smart pointer, for example a boost::shared_ptr?
What could be the best preferable way of returning back a modified list grabbed
What is the best way to construct an #ask query using semantic properties from
I'm reading that using the OUTPUT clause is probably the best way to retrieve
Is this the best way to select the Fruit object I need from the
What is the best way to just run a PHP script when the user
What is the best way to do that? I tried the following: ByteBuffer cacheBuffer=ByteBuffer.allocateDirect(nm(nLimit,0));
What is the best way to parse html tag like that: <a href=/Results>Results</a> i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.