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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:51:59+00:00 2026-06-13T07:51:59+00:00

I need to initialize a boost::shared_ptr based on a condition. Sample code is below

  • 0

I need to initialize a boost::shared_ptr based on a condition. Sample code is below that depicts the scenario i ma looking for.

class A{};
class B:public A{};
class C:public A();

void some_func(int opt)
{
   boost::shared_ptr<A> abstract_ptr;
   if(opt==RELEVENT_TO_B)
   {
      abstract_ptr(new B);
   }
   else
   {
      abstract_ptr(new C);
   }
}

I know, above code sample is wrong. boost::shared_ptr<A> Abstract_ptr in second line of some_func() will create a shared_ptr object with the shared_ptr constrcutor that takes no arguments and since there is no assignment operator defined and no shared_ptr takes raw pointer after initialization so code within if-else is wrong.

My question is, how i can achieve this (conditional initialization as shown in some_func) modestly with boost::shared_ptr.

  • 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-13T07:52:00+00:00Added an answer on June 13, 2026 at 7:52 am

    You could use .reset() to put a raw pointer to the shared pointer.

       boost::shared_ptr<A> abstract_ptr;
       if (opt == RELEVENT_TO_B)
          abstract_ptr.reset(new B);
       else
          abstract_ptr.reset(new C);
    

    You could also directly assign a shared ptr to it:

       boost::shared_ptr<A> abstract_ptr;
       if (opt == RELEVENT_TO_B)
          abstract_ptr = boost::make_shared<B>();
       else
          abstract_ptr = boost::make_shared<C>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to initialize complex javascript code as an string. But that javascript code
I'm writing a web application and need to initialize some parameters that I'm pulling
I have a class: class One def initialize; end end I need to create
I'm looking for a C++ container that's a cross between boost::array, boost::scoped_array and std::vector.
I'm developing a few custom ant tasks that all need to initialize the same
I've got a Boost.Fusion sequence of elements that need to be initialized one each
I need to initialize few objects, it can take a while so I want
I am writing a program in C++ and I need to initialize an array
In some languages, like php, you don't need to manually initialize each dimension of
To initialize an int array with all zeros, do I need to use: int

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.