<bdo id='8rZid'></bdo><ul id='8rZid'></ul>
<legend id='8rZid'><style id='8rZid'><dir id='8rZid'><q id='8rZid'></q></dir></style></legend>

    1. <small id='8rZid'></small><noframes id='8rZid'>

      <tfoot id='8rZid'></tfoot>
    2. <i id='8rZid'><tr id='8rZid'><dt id='8rZid'><q id='8rZid'><span id='8rZid'><b id='8rZid'><form id='8rZid'><ins id='8rZid'></ins><ul id='8rZid'></ul><sub id='8rZid'></sub></form><legend id='8rZid'></legend><bdo id='8rZid'><pre id='8rZid'><center id='8rZid'></center></pre></bdo></b><th id='8rZid'></th></span></q></dt></tr></i><div id='8rZid'><tfoot id='8rZid'></tfoot><dl id='8rZid'><fieldset id='8rZid'></fieldset></dl></div>
      1. 避免繁琐的可选参数

        时间:2023-09-27
          <bdo id='ZVa5o'></bdo><ul id='ZVa5o'></ul>
        • <legend id='ZVa5o'><style id='ZVa5o'><dir id='ZVa5o'><q id='ZVa5o'></q></dir></style></legend>

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

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

                • 本文介绍了避免繁琐的可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果我有一个带有 2 个必需参数和 4 个可选参数的构造函数,我怎样才能避免编写 16 个构造函数,甚至 10 个左右的构造函数,如果我使用默认参数(我不喜欢)因为它的自我文档很差)?是否有任何使用模板的习语或方法可以使我不那么乏味?(而且更容易维护?)

                  If I have a constructor with say 2 required parameters and 4 optional parameters, how can I avoid writing 16 constructors or even the 10 or so constructors I'd have to write if I used default parameters (which I don't like because it's poor self-documentation)? Are there any idioms or methods using templates I can use to make it less tedious? (And easier to maintain?)

                  推荐答案

                  您可能对 命名参数习语.

                  总而言之,创建一个类来保存要传递给构造函数的值.添加一个方法来设置每个值,并让每个方法在最后执行 return *this; .在你的类中有一个构造函数,它接受这个新类的 const 引用.这可以像这样使用:

                  To summarize, create a class that holds the values you want to pass to your constructor(s). Add a method to set each of those values, and have each method do a return *this; at the end. Have a constructor in your class that takes a const reference to this new class. This can be used like so:

                  class Person;
                  
                  class PersonOptions
                  {
                    friend class Person;
                    string name_;
                    int age_;
                    char gender_;
                  
                  public:
                     PersonOptions() :
                       age_(0),
                       gender_('U')
                     {}
                  
                     PersonOptions& name(const string& n) { name_ = n; return *this; }
                     PersonOptions& age(int a) { age_ = a; return *this; }
                     PersonOptions& gender(char g) { gender_ = g; return *this; }
                  };
                  
                  class Person
                  {
                    string name_;
                    int age_;
                    char gender_;
                  
                  public:
                     Person(const PersonOptions& opts) :
                       name_(opts.name_),
                       age_(opts.age_),
                       gender_(opts.gender_)
                     {}
                  };
                  Person p = PersonOptions().name("George").age(57).gender('M');
                  

                  这篇关于避免繁琐的可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么 C++ 没有 const 构造函数? 下一篇:为什么受保护的构造函数会引发此代码的错误?

                  相关文章

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

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

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

                      <tfoot id='uqNiG'></tfoot>