有效的C ++:阻止保护继承? [英] Effective C++: discouraging protected inheritance?

查看:121
本文介绍了有效的C ++:阻止保护继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Scott Meyers的 Effective C ++ (第三版),并在项目32中的一段中:确保公共继承是is-a 151他发表了评论(我用粗体表示):

I was reading Scott Meyers' Effective C++ (third edition), and in a paragraph in Item 32: Make sure public inheritance is "is-a" on page 151 he makes the comment (which I've put in bold):


这只适用于 public 遗产。 C ++将表现为我已经描述,只有当学生是公开派生自人。私人继承意味着完全不同的东西(见第39条),和受保护的继承是我今天的意义。

This is true only for public inheritance. C++ will behave as I've described only if Student is publicly derived from Person. Private inheritance means something entirely different (see Item 39), and protected inheritance is something whose meaning eludes me to this day.

问题:我应该如何解读此评论?

The question: how should I interpret this comment? Is Meyers trying to convey that protected inheritance is seldom considered useful and should be avoided?

(我读过这个问题
C ++中私有,公共和受保护继承之间的差异以及C++ FAQ Lite的私人和受保护的继承部分,两者都解释了什么是受保护的继承意味着什么,但是没有给我很多洞察力何时或为什么它是有用的。)

(I've read the question Difference between private, public and protected inheritance in C++ as well as C++ FAQ Lite's private and protected inheritance section, both of which explain what protected inheritance means, but hasn't given me much insight into when or why it would be useful.)

推荐答案

protected:

Some scenarios where you'd want protected:


  1. 你有一个基类,你知道你从来不想暴露外部的功能,但你知道

  1. You have a base class with methods where you know you never want to expose the functionality outside, but you know will be useful for any derived class.

你有一个基类,其成员应该被任何类扩展的类逻辑使用,但绝不应该暴露

You have a base class with members that should be logically used by any class that extends that class, but should never be exposed outside.

由于多重继承,你可以使用基类的继承类型来构造一个更多的类现有逻辑和实现。

Thanks to multiple inheritance you can play around with base classes' inheritance type and construct a more diversed class with existing logic and implementation.

更具体的示例:

您可以创建几个抽象类按照设计模式逻辑,假设您有:

You could create a few abstract classes that follow Design Pattern logic, lets say you have:

Observer
Subject
Factory

现在你希望这些都是公开的,因为一般来说,你可以使用任何模式。

Now you want these all to be public, because in general, you could use the pattern on anything.

但是,使用受保护的继承,你可以创建一个类Observer和Subject,但只有受保护的工厂,所以工厂部分只用于继承类。 (例如,选择随机模式)

But, with protected inheritance, you can create a class that is an Observer and Subject, but only protected factory, so the factory part is only used in inherited classes. (Just chose random patterns for the example)

另一个示例:

例如,你想继承一个库类(不是我鼓励它)。假设你想让你拥有 std :: list<> 或者更好的扩展 shared_ptr

Lets say for example you want to inherit from a library class (not that I encourage it). Lets say you want to make you own cool extension of std::list<> or a "better" shared_ptr.

您可以从基类(设计为具有公共方法)中得到保护。

这将允许您选择使用自己的自定义方法,使用类的逻辑,并将逻辑传递给任何派生类。

You could derive protectedly from the base class (which is designed to have public methods).
This will give you the option to use your own custom methods, use the class' logic, and pass the logic to the to any derived class.

你可以使用封装,但继承遵循适当的逻辑 IS A
(或在本例中为A类)

You could probably use encapsulation instead, but inheritance follows the proper logic of IS A (or in this case IS sort of A)

这篇关于有效的C ++:阻止保护继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆