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

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

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

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

        可选函数参数:使用默认参数 (NULL) 还是重载函数?

        时间:2023-09-27

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

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

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

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

                • 本文介绍了可选函数参数:使用默认参数 (NULL) 还是重载函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个函数可以处理给定的向量,但如果没有给出,也可以自己创建这样的向量.

                  I have a function that processes a given vector, but may also create such a vector itself if it is not given.

                  对于这种情况,我看到了两种设计选择,其中函数参数是可选的:

                  I see two design choices for such a case, where a function parameter is optional:

                  使其成为一个指针并使其默认为NULL:

                  Make it a pointer and make it NULL by default:

                  void foo(int i, std::vector<int>* optional = NULL) {
                    if(optional == NULL){
                      optional = new std::vector<int>();
                      // fill vector with data
                    }
                    // process vector
                  }
                  

                  或者有两个具有重载名称的函数,其中一个省略了参数:

                  Or have two functions with an overloaded name, one of which leaves out the argument:

                  void foo(int i) {
                     std::vector<int> vec;
                     // fill vec with data
                     foo(i, vec);
                  }
                  
                  void foo(int i, const std::vector<int>& optional) {
                    // process vector
                  }
                  

                  是否有理由更喜欢一种解决方案?

                  Are there reasons to prefer one solution over the other?

                  我稍微喜欢第二个,因为我可以使向量成为 const 引用,因为它在提供时只能读取,不能写入.此外,界面看起来更干净(是不是 NULL 只是一个黑客?).并且可能会优化掉间接函数调用导致的性能差异.

                  I slightly prefer the second one because I can make the vector a const reference, since it is, when provided, only read, not written. Also, the interface looks cleaner (isn't NULL just a hack?). And the performance difference resulting from the indirect function call is probably optimized away.

                  然而,我经常在代码中看到第一个解决方案.除了程序员的懒惰之外,是否有令人信服的理由更喜欢它?

                  Yet, I often see the first solution in code. Are there compelling reasons to prefer it, apart from programmer laziness?

                  推荐答案

                  我绝对喜欢重载方法的第二种方法.

                  I would definitely favour the 2nd approach of overloaded methods.

                  第一种方法(可选参数)模糊了方法的定义,因为它不再有一个明确定义的目的.这反过来又增加了代码的复杂性,让不熟悉它的人更难理解它.

                  The first approach (optional parameters) blurs the definition of the method as it no longer has a single well-defined purpose. This in turn increases the complexity of the code, making it more difficult for someone not familiar with it to understand it.

                  对于第二种方法(重载方法),每种方法都有明确的目的.每种方法都是结构良好内聚.一些附加说明:

                  With the second approach (overloaded methods), each method has a clear purpose. Each method is well-structured and cohesive. Some additional notes:

                  • 如果有代码需要复制到两个方法中,可以提取到一个单独的方法中,每个重载的方法都可以调用这个外部方法.
                  • 我会更进一步,为每个方法命名不同以表明方法之间的差异.这将使代码更具自文档性.
                  • If there's code which needs to be duplicated into both methods, this can be extracted out into a separate method and each overloaded method could call this external method.
                  • I would go a step further and name each method differently to indicate the differences between the methods. This will make the code more self-documenting.

                  这篇关于可选函数参数:使用默认参数 (NULL) 还是重载函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将可修改的参数传递给 C++ 函数 下一篇:我应该对 C++ 中的成员变量和函数参数使用相同的名称吗?

                  相关文章

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

                  2. <small id='evcEj'></small><noframes id='evcEj'>

                    <tfoot id='evcEj'></tfoot>