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

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

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

          <bdo id='pYpB8'></bdo><ul id='pYpB8'></ul>
      2. 函数参数默认值

        Default value of function parameter(函数参数默认值)

        <small id='69z8u'></small><noframes id='69z8u'>

          <tbody id='69z8u'></tbody>
        • <bdo id='69z8u'></bdo><ul id='69z8u'></ul>
              • <legend id='69z8u'><style id='69z8u'><dir id='69z8u'><q id='69z8u'></q></dir></style></legend>
                <tfoot id='69z8u'></tfoot>

                <i id='69z8u'><tr id='69z8u'><dt id='69z8u'><q id='69z8u'><span id='69z8u'><b id='69z8u'><form id='69z8u'><ins id='69z8u'></ins><ul id='69z8u'></ul><sub id='69z8u'></sub></form><legend id='69z8u'></legend><bdo id='69z8u'><pre id='69z8u'><center id='69z8u'></center></pre></bdo></b><th id='69z8u'></th></span></q></dt></tr></i><div id='69z8u'><tfoot id='69z8u'></tfoot><dl id='69z8u'><fieldset id='69z8u'></fieldset></dl></div>
                  本文介绍了函数参数默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  1.

                  int Add (int a, int b = 3);
                  int Add (int a, int b)
                  {
                  
                  }
                  

                  2.

                  int Add (int a, int b);
                  int Add (int a, int b = 3)
                  {
                  
                  }
                  

                  两者都有效;这是标准方式,为什么?

                  Both work; which is the standard way and why?

                  推荐答案

                  如果你把声明放在一个头文件中,而定义在一个单独的 .cpp 文件中,并且 #include 来自不同 .cpp 文件的标题,您将能够看到差异.

                  If you put the declaration in a header file, and the definition in a separate .cpp file, and #include the header from a different .cpp file, you will be able to see the difference.

                  具体来说,假设:

                  int Add(int a, int b);
                  

                  lib.cpp

                  int Add(int a, int b = 3) {
                     ...
                  }
                  

                  test.cpp

                  #include "lib.h"
                  
                  int main() {
                      Add(4);
                  }
                  

                  test.cpp的编译不会看到默认的参数声明,会报错.

                  The compilation of test.cpp will not see the default parameter declaration, and will fail with an error.

                  为此,默认参数定义通常在函数声明中指定:

                  For this reason, the default parameter definition is usually specified in the function declaration:

                  int Add(int a, int b = 3);
                  

                  这篇关于函数参数默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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?)
                      <tbody id='nnN7l'></tbody>
                    <legend id='nnN7l'><style id='nnN7l'><dir id='nnN7l'><q id='nnN7l'></q></dir></style></legend>

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

                          <tfoot id='nnN7l'></tfoot>

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

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