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

      1. <small id='NIheA'></small><noframes id='NIheA'>

        之前的常量还是之后的常量?

        Const before or const after?(之前的常量还是之后的常量?)
        <i id='eURID'><tr id='eURID'><dt id='eURID'><q id='eURID'><span id='eURID'><b id='eURID'><form id='eURID'><ins id='eURID'></ins><ul id='eURID'></ul><sub id='eURID'></sub></form><legend id='eURID'></legend><bdo id='eURID'><pre id='eURID'><center id='eURID'></center></pre></bdo></b><th id='eURID'></th></span></q></dt></tr></i><div id='eURID'><tfoot id='eURID'></tfoot><dl id='eURID'><fieldset id='eURID'></fieldset></dl></div>

        <tfoot id='eURID'></tfoot>
      2. <small id='eURID'></small><noframes id='eURID'>

          • <bdo id='eURID'></bdo><ul id='eURID'></ul>

                    <tbody id='eURID'></tbody>
                1. <legend id='eURID'><style id='eURID'><dir id='eURID'><q id='eURID'></q></dir></style></legend>
                  本文介绍了之前的常量还是之后的常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  首先,您可能知道 const 可用于使对象的数据或指针不可修改,或两者兼而有之.

                  To start you probably know that const can be used to make either an object's data or a pointer not modifiable or both.

                  const Object* obj; // can't change data
                  Object* const obj; // can't change pointer
                  const Object* const obj; // can't change data or pointer
                  

                  但是您也可以使用以下语法:

                  However you can also use the syntax:

                  Object const *obj; // same as const Object* obj;
                  

                  似乎唯一重要的是您将 const 关键字放在星号的哪一侧.就我个人而言,我更喜欢将 const 放在类型的左侧以指定它的数据不可修改,因为我发现它在我从左到右的思维方式中读起来更好,但哪种语法先出现?

                  The only thing that seems to matter is which side of the asterisk you put the const keyword. Personally I prefer to put const on the left of the type to specify it's data is not modifiable as I find it reads better in my left-to-right mindset but which syntax came first?

                  更重要的是,为什么有两种正确的方式来指定 const 数据,以及在什么情况下您更喜欢或需要一种方式而不是另一种方式?

                  More importantly why is there two correct ways of specifying const data and in what situation would you prefer or need one over the other if any?

                  所以这听起来像是一个武断的决定,因为编译器应该如何解释事物的标准早在我出生之前就已经起草好了.由于 const 应用于关键字左侧的内容(默认情况下?)我猜他们认为添加 shortcuts" 来应用关键字和至少在声明通过解析 * 或 & 更改之前以其他方式使用类型限定符....

                  So it sounds like this was an arbitrary decision when the standard for how compilers should interpret things was drafted long before I was born. Since const is applied to what is to the left of the keyword (by default?) I guess they figured there was no harm in adding "shortcuts" to apply keywords and type qualifiers in other ways at least until such a time as the declaration changes by parsing a * or & ...

                  在 C 中也是如此,然后我假设?

                  This was the case in C as well then I'm assuming?

                  推荐答案

                  为什么有两种正确的方法来指定 const 数据,在什么情况下您更喜欢或需要一种方法(如果有)?

                  why is there two correct ways of specifying const data and in what situation would you prefer or need one over the other if any?

                  本质上,说明符中 const 在星号之前的位置无关紧要的原因是 C 语法是由 Kernighan 和 Ritchie 以这种方式定义的.

                  Essentially, the reason that the position of const within specifiers prior to an asterisk does not matter is that the C grammar was defined that way by Kernighan and Ritchie.

                  他们以这种方式定义语法的原因可能是他们的 C 编译器从左到右解析输入,并在每个标记消耗它时完成处理.使用 * 标记会将当前声明的状态更改为指针类型.在* 之后遇到const 表示const 限定符应用于指针声明;在 * 之前遇到它意味着限定符应用于指向的数据.

                  The reason they defined the grammar in this way was likely that their C compiler parsed input from left-to-right and finished processing each token as it consumed that. Consuming the * token changes the state of the current declaration to a pointer type. Encountering const after * means the const qualifier is applied to a pointer declaration; encountering it prior to the * means the qualifier is applied to the data pointed to.

                  因为如果 const 限定符出现在类型说明符之前或之后,语义不会改变,所以无论哪种方式都可以接受.

                  Because the semantic meaning does not change if the const qualifier appears before or after the type specifiers, it is accepted either way.

                  在声明函数指针时会出现类似的情况,其中:

                  A similar sort of case arises when declaring function pointers, where:

                  • void * function1(void) 声明一个返回 void *,

                  • void * function1(void) declares a function which returns void *,

                  void (* function2)(void) 声明一个函数指针,指向一个返回void的函数.

                  void (* function2)(void) declares a function pointer to a function which returns void.

                  再次需要注意的是语言语法支持从左到右的解析器.

                  Again the thing to notice is that the language syntax supports a left-to-right parser.

                  这篇关于之前的常量还是之后的常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)

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

                          • <tfoot id='cGewb'></tfoot>
                              <tbody id='cGewb'></tbody>

                            <legend id='cGewb'><style id='cGewb'><dir id='cGewb'><q id='cGewb'></q></dir></style></legend>

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