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

Extending the C++ Standard Library by inheritance?(通过继承扩展 C++ 标准库?)

本文介绍了通过继承扩展 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++ 标准库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:通过继承扩展 C++ 标准库?

基础教程推荐