保护继承的实际用途是什么? [英] What is the practical use of protected inheritance?

查看:125
本文介绍了保护继承的实际用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

公共继承很容易。



A:public B表示每个A都是B.在大多数编程语言中,如vb.net和objective-c,唯一的继承类型。



私人继承也很简单,但没有意义



但是,这是无意义的,因为这意味着A应该包含B。



任何人都可以向我解释什么地狱是为了?有人说这是一种作为关系。我还是不太清楚。



有没有人有一些样例,人们使用保护继承的良好模式(和良心)的实际生产用途?

解决方案


私人继承也很容易,但毫无意义



A:private B意味着A由B实现。然而,这是无意义的,因为意味着A应该包含B。


您可能不会看到私人继承的原因并不意味着它没有意义。
有几种情况下,私人继承有它的原因。你是对的,乍一看,私人继承意味着有一个关系就像聚合,而私人继承有一个(稍微)更紧密的耦合。



有利于私人继承对于聚集可以是以下的一些:




  • 对于私有继承,你继承typedefs。在某些情况下(例如traits类),继承privatly只是基类中重新typedef吨的typedef的替代。

  • 在很少的情况下,你必须初始化成员 真实(即公共)基类。实现它的唯一方法是使该成员成为在公共基础之前继承的私有基类。

  • 有时你需要访问成员的受保护成员。如果无法更改成员类本身,则必须使用私有继承来访问它们。

  • 如果成员没有自己的数据成员,它仍然占用空间。使它成为一个私有基类,可以实现空基类优化,减少类对象的大小。

  • 甚至更多点,请参阅James下面的注释



这些原因显然是技术原因,有些甚至可能说黑客。然而,这样的原因存在,所以私人继承不是完全没有意义。它只是不是纯OO风格 - 但C ++不是一个纯粹的OO语言。



保护继承的原因很简单,一旦你理解了私人继承:



如果您有理由私下继承某事物,并且希望获得这些福利(例如:会成为您班级的成员, typedefs)可访问派生类,使用受保护的继承。显然,私有继承应该很少使用,并且保护继承甚至更多。


Public inheritance is easy.

A : public B means every A is a B. In most programming language, like vb.net and objective-c, this is the only type of inheritance.

Private inheritance is also easy but pointless

A : private B means A is implemented by B. However, that's pointless because that means A should contain B instead. Ownership means less coupling with no disadvantage.

Then we have protected inheritance.

Can anyone explain to me what the hell is that for? Some says it's an "as a relationship". I am still not very clear on that.

Does anyone has some sample cases where people uses protected inheritance in good pattern (and conscience) for actual productive use?

解决方案

Private inheritance is also easy but pointless

A : private B means A is implemented by B. However, that's pointless because that means A should contain B instead. Ownership means less coupling with no disadvantage.

That you might not see reasons for private inheritance does not mean it's pointless. There are several cases where private inheritance has it's reasons. You are right in that at a first glance, private inheritance means has-a relationships just like aggregation, and that private inheritance has a (slightly) tighter coupling.

Reasons for favouring private inheritance over aggretations could be some of the following:

  • With private inheritance you inherit typedefs as well. In some cases (e.g. traits classes) inheriting privatly is just the alternative to re-typedef tons of typedefs in the base class.
  • In seldom cases you have to initialize a member before a "real" (i.e. public) base class. The only way to achieve that is by making that member a private base class inherited before the public base.
  • Some times you need access to protected members of a member. If you can't change the member class itself, you have to use private inheritance to get access to them.
  • If a member has no data members of its own, it still occupies space. Making it a private base class enables the empty base class optimization, diminuishing the size of your class' objects.
  • for even more points, see James' comments below

These reasons are obviously technical reasons, some might even say "hacks". However, such reasons exist, so private inheritance is not completely pointless. It is just not "pure OO style" - but C++ isn't a pure OO language either.

The reason for protected inheritance are quite simple once you understood the ones for private inheritance:

If you have reasons to inherit something privately and want to make those benefits (i.e. that would-be member of your class or the typedefs) accessible to derived classes, use protected inheritance. Obviously, private inheritance should be used scarcely, and protected inheritance even more so.

这篇关于保护继承的实际用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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