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

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

          <bdo id='J38Wc'></bdo><ul id='J38Wc'></ul>

        为什么在比较中将常量放在变量之前?

        时间:2023-09-27

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

              <bdo id='Dfqd4'></bdo><ul id='Dfqd4'></ul>
                <tbody id='Dfqd4'></tbody>

                <tfoot id='Dfqd4'></tfoot><legend id='Dfqd4'><style id='Dfqd4'><dir id='Dfqd4'><q id='Dfqd4'></q></dir></style></legend>

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

                1. 本文介绍了为什么在比较中将常量放在变量之前?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我注意到我们的一些代码中的以下语法有一段时间了:

                  I noticed for a while now the following syntax in some of our code:

                  if( NULL == var){
                     //...
                  }
                  

                  if( 0 == var){
                    //...
                  }
                  

                  和类似的东西.

                  谁能解释一下为什么写这篇文章的人选择这个符号而不是常见的var == 0方式)?

                  Can someone please explain why did the person who wrote this choose this notation instead of the common var == 0 way)?

                  这是风格问题,还是会以某种方式影响性能?

                  Is it a matter of style, or does it somehow affect performance?

                  推荐答案

                  这是一种避免错误的机制:

                  It's a mechanism to avoid mistakes like this:

                  if ( var = NULL ) {
                    // ...
                  }
                  

                  如果你用右边的变量名编写它,编译器将能够发现某些错误:

                  If you write it with the variable name on the right hand side the compiler will be able catch certain mistakes:

                  if ( NULL = var ) {  // not legal, won't compile
                    // ...
                  }
                  

                  当然,如果变量名出现在等号的两侧并且有些人觉得这种风格不吸引人,那么这当然行不通.

                  Of course this won't work if variable names appear on both sides of the equal sign and some people find this style unappealing.

                  正如 Evan 在评论中提到的,如果您启用警告,任何体面的编译器都会警告您这一点,例如,gcc -Wall 会给您以下信息:

                  As Evan mentioned in the comments, any decent compiler will warn you about this if you enable warnings, for example, gcc -Wall will give you the following:

                  warning: suggest parentheses around assignment used as truth value
                  

                  您应该始终在编译器上启用警告,这是查找错误最便宜的方法.

                  You should always enable warnings on your compiler, it is the cheapest way to find errors.

                  最后,正如 Mike B 所指出的,这是一个风格问题,不会影响程序的性能.

                  Lastly, as Mike B points out, this is a matter of style and doesn't affect the performance of the program.

                  这篇关于为什么在比较中将常量放在变量之前?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么未声明 NULL? 下一篇:为什么这会调用默认构造函数?

                  相关文章

                  <tfoot id='UOsW8'></tfoot>

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

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