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

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

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

      通过继承扩展 C++ 标准库?

      时间:2024-05-12

        <tbody id='YDFij'></tbody>

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

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

            <tfoot id='YDFij'></tfoot>
          • <legend id='YDFij'><style id='YDFij'><dir id='YDFij'><q id='YDFij'></q></dir></style></legend>

                <bdo id='YDFij'></bdo><ul id='YDFij'></ul>
                本文介绍了通过继承扩展 C++ 标准库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                人们普遍认为 C++ 标准库通常不打算使用继承进行扩展.当然,我(和其他人)批评了那些建议从 std::vector 等类派生的人.但是,这个问题:c++ 异常,可以 what() 为 NULL?我意识到标准库中至少有一部分是要扩展的 - std::exception.

                It is a commonly held belief that the the C++ Standard library is not generally intended to be extended using inheritance. Certainly, I (and others) have criticised people who suggest deriving from classes such as std::vector. However, this question: c++ exceptions, can what() be NULL? made me realise that there is at least one part of the Standard Library that is intended to be so extended - std::exception.

                所以,我的问题有两个部分:

                So, my question has two parts:

                1. 是否有其他标准库类要派生自?

                1. Are there any other Standard Library classes which are intended to be derived from?

                如果确实是从标准库类(例如std::exception)派生的,那么是否受 ISO 标准中描述的接口的约束?例如,一个使用异常类的程序是否符合标准?

                If one does derive from a Standard Library class such as std::exception, is one bound by the interface described in the ISO Standard? For example, would a program which used an exception class who's what() member function did not return a NTBS (say it returned a null pointer) be standard conforming?

                推荐答案

                好问题.我真的希望标准更明确地说明预期用途是什么.也许应该有一个 C++ Rationale 文档与语言标准放在一起.无论如何,这是我使用的方法:

                Good nice question. I really wish that the Standard was a little more explicit about what the intended usage is. Maybe there should be a C++ Rationale document that sits alongside the language standard. In any case, here is the approach that I use:

                (a) 我不知道存在任何此类列表.相反,我使用以下列表来确定标准库类型是否可能被设计为继承自:

                (a) I'm not aware of the existence of any such list. Instead, I use the following list to determine whether a Standard Library type is likely to be designed to be inherited from:

                • 如果它没有任何 virtual 方法,那么您不应该将它用作基础.这排除了 std::vector 等.
                • 如果它确实有 virtual 方法,那么它可以用作基类.
                • 如果有很多 friend 语句,那么请避开,因为可能存在封装问题.
                • 如果它是一个模板,那么在继承它之前请仔细查看,因为您可能可以使用专门化对其进行自定义.
                • 基于策略的机制(例如,std::char_traits)的存在是一个很好的线索,表明您不应该将其用作基础.
                • If it doesn't have any virtual methods, then you shouldn't be using it as a base. This rules out std::vector and the like.
                • If it does have virtual methods, then it is a candidate for usage as a base class.
                • If there are lots of friend statements floating around, then steer clear since there is probably an encapsulation problem.
                • If it is a template, then look closer before you inherit from it since you can probably customize it with specializations instead.
                • The presence of policy-based mechanism (e.g., std::char_traits) is a pretty good clue that you shouldn't be using it as a base.

                不幸的是,我不知道一个很好的综合列表或黑白列表.我通常凭直觉判断.

                Unfortunately I don't know of a nice comprehensive or black and white list. I usually go by gut feel.

                (b) 我会在这里应用 LSP.如果有人对您的异常调用 what(),那么它的可观察行为应该与 std::exception 的行为相匹配.我认为这与其说是正确性问题,不如说是真正的标准一致性问题.该标准不要求子类可以替代基类.这实际上只是一种最佳实践".

                (b) I would apply LSP here. If someone calls what() on your exception, then it's observable behavior should match that of std::exception. I don't think that it is really a standards conformance issue as much as a correctness issue. The Standard doesn't require that subclasses are substitutable for base classes. It is really just a "best practice".

                这篇关于通过继承扩展 C++ 标准库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:为什么 std::vector 和 std::array 的 C++ initializer_list 行为不同? 下一篇:字符串 c_str() 与 data()

                相关文章

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

                <tfoot id='9rIwf'></tfoot><legend id='9rIwf'><style id='9rIwf'><dir id='9rIwf'><q id='9rIwf'></q></dir></style></legend>
              • <small id='9rIwf'></small><noframes id='9rIwf'>

                    <bdo id='9rIwf'></bdo><ul id='9rIwf'></ul>