1. <tfoot id='GvUh1'></tfoot><legend id='GvUh1'><style id='GvUh1'><dir id='GvUh1'><q id='GvUh1'></q></dir></style></legend>

        <bdo id='GvUh1'></bdo><ul id='GvUh1'></ul>
      1. <small id='GvUh1'></small><noframes id='GvUh1'>

        <i id='GvUh1'><tr id='GvUh1'><dt id='GvUh1'><q id='GvUh1'><span id='GvUh1'><b id='GvUh1'><form id='GvUh1'><ins id='GvUh1'></ins><ul id='GvUh1'></ul><sub id='GvUh1'></sub></form><legend id='GvUh1'></legend><bdo id='GvUh1'><pre id='GvUh1'><center id='GvUh1'></center></pre></bdo></b><th id='GvUh1'></th></span></q></dt></tr></i><div id='GvUh1'><tfoot id='GvUh1'></tfoot><dl id='GvUh1'><fieldset id='GvUh1'></fieldset></dl></div>

        if 语句中变量的范围

        Scope of variables in if statements(if 语句中变量的范围)
        <legend id='Ag9TQ'><style id='Ag9TQ'><dir id='Ag9TQ'><q id='Ag9TQ'></q></dir></style></legend>

            <small id='Ag9TQ'></small><noframes id='Ag9TQ'>

            <i id='Ag9TQ'><tr id='Ag9TQ'><dt id='Ag9TQ'><q id='Ag9TQ'><span id='Ag9TQ'><b id='Ag9TQ'><form id='Ag9TQ'><ins id='Ag9TQ'></ins><ul id='Ag9TQ'></ul><sub id='Ag9TQ'></sub></form><legend id='Ag9TQ'></legend><bdo id='Ag9TQ'><pre id='Ag9TQ'><center id='Ag9TQ'></center></pre></bdo></b><th id='Ag9TQ'></th></span></q></dt></tr></i><div id='Ag9TQ'><tfoot id='Ag9TQ'></tfoot><dl id='Ag9TQ'><fieldset id='Ag9TQ'></fieldset></dl></div>
            • <bdo id='Ag9TQ'></bdo><ul id='Ag9TQ'></ul>

                <tbody id='Ag9TQ'></tbody>

              <tfoot id='Ag9TQ'></tfoot>

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

                  问题描述

                  我有一个没有默认构造函数或赋值运算符的类,因此它在 if/else 语句中声明和初始化,具体取决于另一个函数的结果.但是后来它说它超出了范围,即使条件的两条路线都将创建一个实例.

                  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.

                  考虑以下示例(使用 int 完成只是为了说明这一点):

                  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?

                  编辑

                  根据给出的答案,情况更为复杂,因此可能必须改变方法.有一个抽象基类 A 和两个从 A 派生的类 B 和 C.这样的事情会怎样:

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

                  改变方法?由于 A 是抽象的,我不能只声明 A* obj 并使用 new 创建适当的类型.

                  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
                  

                  就您而言,假设您有 class A.

                  In your case, say you have class A.

                  如果您不处理指针:

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

                  是的,你可以:

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

                  看来你要找的是工厂模式(查一下):

                  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 语句中变量的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)
                2. <tfoot id='30WAp'></tfoot>
                    <i id='30WAp'><tr id='30WAp'><dt id='30WAp'><q id='30WAp'><span id='30WAp'><b id='30WAp'><form id='30WAp'><ins id='30WAp'></ins><ul id='30WAp'></ul><sub id='30WAp'></sub></form><legend id='30WAp'></legend><bdo id='30WAp'><pre id='30WAp'><center id='30WAp'></center></pre></bdo></b><th id='30WAp'></th></span></q></dt></tr></i><div id='30WAp'><tfoot id='30WAp'></tfoot><dl id='30WAp'><fieldset id='30WAp'></fieldset></dl></div>

                    <small id='30WAp'></small><noframes id='30WAp'>

                      <bdo id='30WAp'></bdo><ul id='30WAp'></ul>

                          <tbody id='30WAp'></tbody>
                          <legend id='30WAp'><style id='30WAp'><dir id='30WAp'><q id='30WAp'></q></dir></style></legend>