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 1039419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:05:50+00:00 2026-05-16T15:05:50+00:00

Imagine I create an instance of Foo on the heap in a method/function and

  • 0

Imagine I create an instance of Foo on the heap in a method/function and pass it to the caller. What kind of smartpointer would I use?

smartptr new_foo() {
    smartptr foo = new Foo();
    return foo;
}

void bar() {
    smartptr foo = new_foo();
    foo->do_something();
    // now autodelete foo, don't need it anymore
}

Ok… now: As far as I understand those smartpointers from boost, scoped_ptr should be the one to be used in bar(). But I can’t create it in foo(), as it’s not copyable. So I have to create a shared_ptr in foo() and return it. But do I now have to use a shared_ptr in bar(), or can I just “cast” it to an shared_ptr in bar()?

Edit

Thanks for your answers so far! So now I got two solutions:

  • Use only boost::shared_ptr
  • Use std::auto_ptr in the generator foo() and boost::shared_ptr in bar()

Sincerely, I prefer a boost-only solution and won’t mix it with STL except there is really a good reason for doing so. So, next question: has the mixed solution any advantage over the boost only solution?

  • 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-05-16T15:05:50+00:00Added an answer on May 16, 2026 at 3:05 pm

    boost docs suggest they don’t mix

    If you are transfering ownership then another option at the moment would be auto_ptr e.g.

    smartptr new_foo() { 
        std::auto_ptr<Foo> foo = new Foo(); 
        return foo; 
    } 
    
    void bar() { 
        std::auto_ptr<Foo> foo = new_foo(); 
        foo->do_something(); 
        // now autodelete foo, don't need it anymore 
    } 
    

    however this will then restrict how you can use new_foo() i.e. you can’t use it to fill a container (without managing the resource some other way). 🙁

    With C++0x and rvalue references this will be a job for unique_ptr, which will replace auto_ptr and just do what you want

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

Sidebar

Related Questions

Imagine an interface with a method to create objects of type Address . The
Imagine some kind of a banking application, with a screen to create accounts. Each
I want to create a helper method that I can imagine has a signature
Is it possible to create an extension method that returns the instance that is
I am trying to write a factory method that will create a derived instance
Imagine C# code like this ISomeInterface someVar = creator.Create(typeof(SomeClass), typeof(ISomeInterface)); What should happen? Method
Imagine there is a Customer class with an instance Load() method. When the Load()
Let's imagine I have Fraction class. So, the correct way to create instance of
I would like a macro or method to find and replace instance of a
Imagine that I want to create an array from another array like this: $array

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.