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

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

      <tfoot id='bpn7p'></tfoot>
          <bdo id='bpn7p'></bdo><ul id='bpn7p'></ul>
        <legend id='bpn7p'><style id='bpn7p'><dir id='bpn7p'><q id='bpn7p'></q></dir></style></legend>

        如何处理构造函数中的错误值?

        How to handle incorrect values in a constructor?(如何处理构造函数中的错误值?)

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

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

                  问题描述

                  请注意,这是关于构造函数的问题,而不是关于处理时间的类.

                  假设我有一个这样的类:

                  Suppose I have a class like this:

                  class Time
                  {
                  protected:
                      unsigned int m_hour;
                      unsigned int m_minute;
                      unsigned int m_second;
                  public:
                      Time(unsigned int hour, unsigned int minute, unsigned int second);
                  };
                  

                  虽然我希望 a 构造成功,但我希望 b 的构造函数失败.

                  While I would want a to be constructed successfully, I would want the constructor of b to fail.

                  Time a = Time(12,34,56);
                  Time b = Time(12,34,65); // second is larger than 60
                  

                  然而,这是不可能的,因为构造函数不返回任何值并且总是会成功.

                  However, this is not possible, because constructors do not return any values and will always succeed.

                  构造函数如何告诉程序它不开心?我想到了几个方法:

                  How would the constructor tell the program that it is not happy? I have thought of a few methods:

                  1. 让构造函数抛出异常,并在调用函数中使用处理程序来处理它.
                  2. 在类中有一个标志,只有在构造函数可接受值时才将其设置为 true,并让程序在构造后立即检查标志.
                  3. 在调用构造函数之前立即调用一个单独的(可能是静态的)函数来检查输入参数.
                  4. 重新设计该类,使其可以从任何输入参数构建.

                  以下哪种方法在工业中最常见?或者我可能遗漏了什么?

                  Which of these methods is most common in industry? Or is there anything I may have missed?

                  推荐答案

                  典型的解决方案是抛出异常.

                  The typical solution is to throw an exception.

                  其背后的逻辑如下:构造函数是一种将内存块转换为有效对象的方法.要么它成功(正常完成)并且您有一个有效的对象,要么您需要一些不可忽视的问题指示符.异常是使问题在 C++ 中不可忽略的唯一方法.

                  The logic behind that is the following: the constructor is a method that transforms a chunk of memory into a valid object. Either it succeeds (finishes normally) and you have a valid object or you need some non-ignorable indicator of a problem. Exceptions are the only way to make the problem non-ignorable in C++.

                  这篇关于如何处理构造函数中的错误值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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?)
                    <legend id='o9wao'><style id='o9wao'><dir id='o9wao'><q id='o9wao'></q></dir></style></legend>
                      <i id='o9wao'><tr id='o9wao'><dt id='o9wao'><q id='o9wao'><span id='o9wao'><b id='o9wao'><form id='o9wao'><ins id='o9wao'></ins><ul id='o9wao'></ul><sub id='o9wao'></sub></form><legend id='o9wao'></legend><bdo id='o9wao'><pre id='o9wao'><center id='o9wao'></center></pre></bdo></b><th id='o9wao'></th></span></q></dt></tr></i><div id='o9wao'><tfoot id='o9wao'></tfoot><dl id='o9wao'><fieldset id='o9wao'></fieldset></dl></div>

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

                      <tfoot id='o9wao'></tfoot>

                        • <bdo id='o9wao'></bdo><ul id='o9wao'></ul>
                            <tbody id='o9wao'></tbody>