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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:55:13+00:00 2026-06-13T12:55:13+00:00

Suppose I had two shared_ptr types such as boost::shared_ptr<ObjA> sptrA; boost::shared_ptr<ObjB> sptrB; Now suppose

  • 0

Suppose I had two shared_ptr types such as

boost::shared_ptr<ObjA> sptrA;
boost::shared_ptr<ObjB> sptrB;

Now suppose that sptrA->SomeMethod() returned a simple ObjB type (not a shared ptr). Is it possible for me to store that type somehow in sptrB ? So that I could do something like this so that the returned type instance is automatically converted to boost_shared ptr

sptrB = sptrA->SomeMethod(); 

I asked this question just of curiosity and whether it is possible or not ?

  • 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-13T12:55:14+00:00Added an answer on June 13, 2026 at 12:55 pm

    The most standard way of creating boost:shared_ptr objects is to use the make_shared function provided by Boost:

    #include <boost/shared_ptr.hpp>
    #include <boost/make_shared.hpp>
    
    struct A {};
    
    A generator() {
      return A();
    }
    
    int main()
    {
      using namespace boost;
      shared_ptr<A> p = make_shared<A>(generator());
      return 0;
    }
    

    Since the generator() function returns an A object by value, the syntax above implies that new is invoked with the copy contructor of A, and the resulting pointer is wrapped in a shared-pointer object. In other words, make_shared doesn’t quite perform a conversion to shared pointer; instead, it creates a copy of the object on the heap and provides memory management for that. This may or may not be what you need.


    Note that this is equivalent to what std::make_shared does for std::shared_ptr in C++11.


    One way to provide the convenient syntax you mentioned in your question is to define a conversion operator to shared_ptr<A> for A:

    struct A {
      operator boost::shared_ptr<A>() {
        return boost::make_shared<A>(*this);
      }
    };
    

    Then you can use it as follows:

    shared_ptr<A> p = generate();
    

    This will automatically “convert” the object returned by the function. Again, conversion here really means heap allocation, copying and wrapping in a shared pointer. Therefore, I am not really sure if I’d recommend defining such a convenience conversion operator. It makes the syntax very convenient, but it, as all implicit conversion operators, may also mean that you implicitly cause these “conversions” to happen in places you didn’t expect.

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

Sidebar

Related Questions

Suppose I had two brushes. One that was a linear gradient brush that was
Suppose I had a WCF service that I have coded up, like Clemens Vasters's
I had two computers that are located in different parts of the world (Russia
Suppose you had the mySQL table describing if you can mix two substances Product
I have two PCs both with XE2. I thought that I had installed identically
Suppose I had a list my_list = ['91 9925479326','18002561245','All the best','good'] Now I want
Suppose you have an application that consists of two layers: A: A data layer
Suppose we had two arrays: some values, e.g. array([1.2, 1.4, 1.6]) , and some
Suppose I had the string 1122345 There are two pairs in this string. What's
Suppose I had a string string1 = 498results should get Now I need to

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.