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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:41:08+00:00 2026-05-29T07:41:08+00:00

When using shared_ptr , should I just use the shared_ptr declaration once or declare

  • 0

When using shared_ptr, should I just use the shared_ptr declaration once or declare shared_ptr everywhere I pass it?

So in the function where I new up the instance I wrap it in a shared_ptr but when I return it from the function I could also return a shared_ptr or, using the get() on the shared_ptr, just return a normal pointer.

So my question is, should I just use shared_ptr<myType> when I new the instance and then pass normal pointers around or should I be passing shared_ptr<myType> everywhere?

  • 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-29T07:41:09+00:00Added an answer on May 29, 2026 at 7:41 am

    Creating a shared_ptr doesn’t imbue magical powers on its pointee object. The magic is all in the shared_ptr — and its copies — itself. If you stop using it, you lose your reference counting; worse, because you used it at some point, the object will be automatically deleted when you don’t expect it.

    The whole point of having shared_ptr is that you know your object won’t get destroyed when you’re still using it somewhere.

    In the following:

    T* foo() {
       shared_ptr<T> sp(new T());
       return sp.get();
       // ^ the only shared_ptr<T> referencing the obj is dead;
       // obj is deleted;
       // returned pointer invalid before you can even do anything with it
    }
    

    your pointer is immediately invalid.

    There may well be circumstances in which you extract a raw pointer, but these should be rare. If you are in a function where you know you don’t need the reference counting, then just pass the shared_ptr in by reference.

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

Sidebar

Related Questions

will using a shared_ptr on class instance members clean up the new'ed objects correctly
When using the pImpl idiom is it preferable to use a boost:shared_ptr instead of
I am trying to get up to speed with using shared_ptr, but I can't
Are there any downsides with using make_shared<T>() instead of using shared_ptr<T>(new T) . Boost
I have a C++ RAII class for managing Win32 HANDLEs using boost::shared_ptr<> that looks
I have the following code: #include <iostream> #include boost/shared_ptr.hpp using boost::shared_ptr; class Base {
How much do using smart pointers, particularly boost::shared_ptr cost more compared to bare pointers
I know that I can share files using Shared Folders in Virtual PC, but
In my JNI code, I am only using boost/share_ptr.h But I didn't know which
What are the best practices for using autoconf in conjunction with shared_ptr and other

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.