if语句中变量的范围 [英] Scope of variables in if statements

查看:711
本文介绍了if语句中变量的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个没有默认构造函数或赋值运算符的类,所以它根据另一个函数的结果在if / else语句中声明和初始化。但是它说它是在范围之后,即使条件的两个路由将创建一个实例。



考虑下面的例子(使用 int 来说明这一点):

  #include< iostream> 

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

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

在条件语句中声明的变量超出范围?处理没有默认构造函数但构造函数的参数取决于某些条件的正确方法是什么?



编辑 / p>

根据给出的答案,情况更复杂,因此也许该方法必须改变。有一个抽象基类A和从A派生的两个类B和C.这将是什么样子:

  if条件){
B obj(args);
} else {
C obj(args);
}

改变方法?由于A是抽象的,我不能只声明 A * obj 并创建一个具有 new 的适当类型。

解决方案

在条件结束时声明的变量是否超出范围?



- 本地变量的范围仅在括号中:

  {
int x; // scope begins

// ...
} //范围结束
// x在此不可用

在您的情况下,假设您有 A类


$ b b

如果你不处理指针:

  A a(condition?1:2); 

或如果您使用不同的构造函数原型:

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

如果您在堆上创建实例:

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

或者您可以使用三元运算符:

  //如果条件为真,调用A(1),否则A(2)
A * instance = new A );

编辑:



  A * x = NULL; //指向抽象类的指针 - 它工作
if(condition)
x = new B();
else
x = new C();

编辑:



'查找是工厂模式(查找):

  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;
}
};


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.

解决方案

"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 = true )
{
   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;
    }
 };

这篇关于if语句中变量的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆