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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:52:03+00:00 2026-06-10T00:52:03+00:00

The similar questions here all seem to use boost, which I’m not using. What

  • 0

The similar questions here all seem to use boost, which I’m not using.

What I’m trying to do is demonstrated by the following:

In the “owner”:

std::shared_ptr<State> m_state;

m_state = make_shared<State>(param);

m_state = m_state->SomeVirtualFunction();    // The original m_state object gets destroyed

In the “owned”:

std::shared_ptr<State> State::SomeVirtualFunction() {
    return std:shared_ptr<State>(this);
}

In Visual C++ in MSVS 2012, the owned object gets destroyed. How can I keep it alive?

  • 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-10T00:52:04+00:00Added an answer on June 10, 2026 at 12:52 am

    You need to inherit from std::enable_shared_from_this; see What is the usefulness of `enable_shared_from_this`?. std::enable_shared_from_this equips your type with a member function shared_from_this that you call instead of std::shared_ptr<State>(this):

    std::shared_ptr<State> State::SomeVirtualFunction() {
        return shared_from_this();
    }
    

    Prior to C++11 (or Boost, which is where C++11 got enable_shared_from_this from), and assuming that you have a shared_ptr implementation that doesn’t provide enable_shared_from_this, you can do this manually by giving State a weak_ptr to itself that it can convert to a shared_ptr when it needs to:

    class State {
        ...
        std::weak_ptr<State> weak_self;
    };
    
    m_state = make_shared<State>(param);
    m_state->weak_self = m_state;
    
    std::shared_ptr<State> State::SomeVirtualFunction() {
        return weak_self.lock()
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there's similar questions on here, but they all seem to end up
I've reviewed all the similar questions here, but I can't for the life of
First of all, I know there are a few quite similar questions here on
First of all I know similar questions has been asked here, I've checked but
I've looked at a couple of similar questions here but I still can's seem
A similar question was asked here , but the answers generally all seem to
there seem to be a lot of threads here asking similar questions, but I'm
I've read similar questions here, but am still not clear on a couple of
I've looked at similar questions here but I haven't found one particular to my
I have seen similar questions here but I just can't understand them. I am

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.