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

  • Home
  • SEARCH
  • 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 6948621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:50:49+00:00 2026-05-27T13:50:49+00:00

I have a class that has no default constructor or assignment operator so it

  • 0

I have a class that has no default constructor or assignment operator so it is declared and initialized within an if/else statement depending on the result of another function. But then it says that it is out of scope later even though both routes of the conditional will create an instance.

Consider the following example (done with int just to illustrate the point):

#include <iostream>

int main() 
{
  if(1) {
    int i = 5;
  } else {
    int i = 0;
  }

  std::cout << i << std::endl;
  return 0;
}

Do variables declared in a conditional go out of scope at the end of the conditional? What is the correct way to handle the situation where there is no default constructor but the arguments for the constructor depend on certain conditionals?

Edit

In light of the answers given, the situation is more complex so maybe the approach would have to change. There is an abstract base class A and two classes B and C that derive from A. How would something like this:

if(condition) {
   B obj(args);
} else {
   C obj(args);
}

change the approach? Since A is abstract, I couldn’t just declare A* obj and create the appropriate type with new.

  • 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-27T13:50:49+00:00Added an answer on May 27, 2026 at 1:50 pm

    “Do variables declared in a conditional go out of scope at the end of the conditional?”

    Yes – the scope of a local variable only falls within enclosing brackets:

    {
       int x; //scope begins
    
       //...
    }//scope ends
    //x is not available here
    

    In your case, say you have class A.

    If you’re not dealing with pointers:

    A a( condition ? 1 : 2 );
    

    or if you’re using a different constructor prototype:

    A a = condition ? A(1) : A(2,3);
    

    If you’re creating the instance on the heap:

    A* instance = NULL;
    if ( condition )
    {
       instance = new A(1);
    }
    else
    {
       instance = new A(2);
    }
    

    or you could use the ternary operator:

    //if condition is true, call A(1), otherwise A(2)
    A* instance = new A( condition ? 1 : 2 );
    

    EDIT:

    Yes you could:

    A* x = NULL; //pointer to abstract class - it works
    if ( condition )
       x = new B();
    else
       x = new C();
    

    EDIT:

    It seems what you’re looking for is the factory pattern (look it up):

     class A; //abstract
     class B : public A;
     class C : public A;
    
     class AFactory
     {
     public:
        A* create(int x)
        {
           if ( x == 0 )
              return new B;
           if ( x == 1 )
              return new C;
           return NULL;
        }
     };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class obj1 that has no default constructor, and class obj2 that
I have a class (Uniform) that has a constructor with 2 parameters, and a
I have a class which has not default constructor. And I need a way
I have a class that has a constructor. The constructor passes a param to
I have a class that has a Generic type G In my class model
I have a class that has some properties. And I want something that calculates
I have a class that has a vector of another class objects as a
I have a class that has an inner struct/class (I have decided which yet,
I have a class that has a property that I need to stub. I
I have a class that has a method: -(NSInteger) getCityCountForState:(NSString *)state CityArray:(NSMutableArray *)cityArray {

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.