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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:29:22+00:00 2026-05-26T22:29:22+00:00

I am currently reading through C++ TR1 extensions and started focussing on std::tr1::shared_ptr. So,

  • 0

I am currently reading through C++ TR1 extensions and started focussing on std::tr1::shared_ptr.

So, I read so far that I can declare and initialize shared_ptr<> with this code:

class foo {};
std::tr1::shared_ptr<foo> fsp(new foo);
std::tr1::shared_ptr<foo> fps2(fsp);    // (1) init using first sp

Now I read about enable_shared_from_this ( http://msdn.microsoft.com/en-us/library/bb982611%28v=VS.90%29.aspx ) and see this example:

class foo : public enable_shared_from_this<foo> {};
std::tr1::shared_ptr<foo> fsp(new foo);
std::tr1::shared_ptr<foo> fps2 = fsp->shared_from_this(); // (2) init using first sp

My question is, why would I want to use shared_from_this in comparison to the initalization I labeled as “(1) init using first sp”.

I have read the article What is the usefulness of `enable_shared_from_this`? and now better understand the usefulness of it.

But that leaves me open whether my “(1) init using first sp” is ok or what downsides I could face using it.

  • 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-26T22:29:23+00:00Added an answer on May 26, 2026 at 10:29 pm

    enable_shared_from_this is most useful in class implementation whose objects will be shared. You want to share the same reference counter for all instances of shared_ptrs of your object (is usually done by copying shared_ptr), but class implementation doesn’t have any to make a copy. In this case you can use shared_from_this.

    Consider:

    struct A;
    
    void f(shared_ptr<A> const&) {...}
    
    struct A: public enable_shared_from_this<A>
    {
        void a_f()
        {
            // you need to call f() here passing itself as a parameter
            f(shared_from_this());
        }
    };
    
    shared_ptr<A> a_ptr(new A());
    a_ptr->a_f(); // in inner call to f() will be used temporary shared_ptr<A> 
    // that uses (shares) the same reference counter as a_ptr 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have started to learn about python and is currently reading through a script
So I'm currently working on an ePub reader application, and I've been reading through
I was reading through some source code recently that updated the code on a
I am currently reading through a proposal, where this person is proposing to use
I'm currently reading through the paper Programming with Arrows by John Hughes and I'm
I'm new to ASP.net and I'm currently reading about the SqlDataSource and running through
I am just getting started using Spring and am currently reading Spring in Action
I'm currently reading through C# 4.0 In A Nutshell, and in chapter 2 it
I'm currently reading through the excellent Library for Double-Double and Quad-Double Arithmetic paper, and
I've been reading about path-finding algorithms and I'm currently looking for one that works

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.