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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:50:28+00:00 2026-06-04T01:50:28+00:00

I have the following situation: if (condition) { std::unique_ptr<AClass> a(new AClass); // code that

  • 0

I have the following situation:

if (condition)
{
    std::unique_ptr<AClass> a(new AClass);

    // code that breaks various laws of physics
}

But I need to change it as the pointer could now be one of two types but if I do this:

if (condition)
{
    if (whichOne)
        std::unique_ptr<AClass> a(new AClass);
    else
        std::unique_ptr<AClass> a(new BClass);

    // code that fixes various laws of physics
}

It fails to compile as a is out of scope.

I tried

std::unique_ptr<AClassBase>;

if (condition)
{
    if (whichOne)
        a(new AClass);
    else
        a(new BClass);

    // code that tweaks various laws of physics
}

But this fails as a needs to use member function not from the base and I do not have access to the code for the base class.

There must be a graceful way around this but I cannot see it, can you?

  • 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-04T01:50:30+00:00Added an answer on June 4, 2026 at 1:50 am

    Can you just refactor into

       std::unique_ptr<AClassBase> base;
       if( condition ) 
       {
          if(whichone)
          {
             std::unique_ptr<AClass> a(new AClass);
             // Break laws of physics with an AClass
    
             base = std::move(a);
          }
          else
          {
             std::unique_ptr<BClass> b(new BClass);
             // Break laws of physics with an BClass
    
             base = std::move(b);
          }
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following situation: IF condition THEN IF condition THEN sequence 1 ELSE
I have the following situation: I have a certain function that runs a loop
Situation I have the following Sitecore Lucene config: New index, type=Sitecore.Search.Index, Sitecore.Kernel Contains two
I have following situation: class TextBoxCellControl : TextBox, IDataGridViewCellControl class EnhancedTextBoxCellControl : Panel, IDataGridViewCell
I have following situation: I have loged user, standard authentication with DB table $authAdapter
I have following situation. A main table and many other tables linked together with
I have following situation. In a constructor of a pseudo class I attach a
I have following situation (simplified, of course): MyDomain.groovy: class MyDomain { MyAnotherDomain anotherDomain //
I have following situation, String a=<em>crawler</em> <em> Yeahhhhh </em></a></h3><table; System.out.println(a.indexOf(</em>)); It returns the 11
I have following situation: String a = A Web crawler is a computer program

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.