什么是私有继承,它解决哪些问题?
有人可以解释一下 C++ 中私有/受保护继承的具体用途是什么,以及它旨在解决什么问题吗?
class Bar { };
class Foo : private Bar { };
我已经看过这个问题但我仍然不明白它是什么,更不用说我什么时候应该使用它了。
(与 Java/C# 或类似语言中的功能进行比较也可能会有所帮助。)
Could someone explain what exactly private/protected inheritance in C++ is for, and what problem(s) it is intended to solve?
class Bar { };
class Foo : private Bar { };
I've already seen this question but I still don't understand what it is, let alone when I should use it.
(Comparison to features in Java/C# or similar languages could also be helpful.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
私有继承模型“根据...实现”。含义与“has-a”类似。区别在于:
使用私有继承,您不需要编写包装器(适合懒惰的程序员)
“has-a”允许您更好的控制,您可以仅公开接口的子集,或者根据需要更改方法名称。
私有继承使异常安全变得困难,请查看Exceptional C++以获取更多信息
当您确实需要私有继承时,想要使用基类的受保护成员。
有时在混合类中使用私有继承(有效的c++内存管理章节)
我个人更喜欢使用“has” -a" 出于一般目的,我在排除其他选项后立即使用私有继承。
The private inheritance models "is-implemented-in-terms-of". The meaning is similar to "has-a". The differences are:
With private inheritance you don't need to write a wrapper (good for lazy programmers)
"has-a" allows you better control, you can expose only a subset of the interface or change method names if you like.
Private inheritance makes exception safety difficult, have a look at exceptional c++ for more information
You really need private inheritance just when you want to use a protected members of your base class.
Sometimes private inheritance is used in the mix-in class (Effective c++ memory management chapters)
My personal preference is using "has-a" for general purpose, I use private inheritance just after I have rule out other options.
要了解私有继承,您必须首先了解公共继承。
如果您有以下情况:
这意味着允许以下情况:
您可以采用派生类并获取对基类的引用(或指针)。您还可以这样做:
在
Base
中公开的内容在Pub
中公开。公共继承意味着任何地方的每个人都可以访问驱动类的基类组件。
私有继承有点不同:
最后一行通常不起作用。私有继承意味着只有可以执行此操作的函数和类要么是
Priv
的直接成员,要么是Priv
的友元。如果您在Priv
成员或朋友之外编写此代码,它将无法编译。同样:
只有在
Priv
的成员或好友中才有效。私有继承意味着只有派生类的成员或朋友可以访问基类组件。所以成员函数可以调用派生类的基类部分,但外界没有人可以。
当您公开从另一个类派生一个类时,您是在说派生类“是”基类。它正是其中的一种。当您从基类私有派生时,您就向外界隐藏了该派生。这意味着您正在使用基类中的实现来实现派生类,但其他人不需要知道它。派生类是“根据”基类“实现的”。
To understand private inheritance, you must first understand public inheritance.
If you have the following:
This means that the following is allowed:
You can take the derived class and get a reference (or pointer) to a base class. You can also do this:
That which was public in
Base
is public inPub
.Public inheritance means that everyone, everywhere can access the base class components of the drived class.
Private inheritance is a bit different:
That last line does not work in general. Private inheritance means that the only functions and classes that can perform this operation are either direct members of
Priv
or friends ofPriv
. If you write this code outside of aPriv
member or friend, it will fail to compile.Similarly:
Only works if this is within a member or friend of
Priv
.Private inheritance means that only members or friends of the derived class can access the base class components. So member functions can call the base class part of the derived class, but nobody in the outside world can.
When you publicly derive a class from another class, you are saying that the derived class "is a" base class. It is exactly one of those types. When you privately derive from a base class, you are hiding the derivation from the outside world. This means that you are using the implementation in the base class to implement your derived class, but nobody else needs to know it. The derived class is "implemented in terms of" the base class.
私有继承是实现类组合的一种方式。此 C++ 常见问题解答有一整章关于此主题,请参阅 http://www .parashift.com/c++-faq-lite/private-inheritance.html。
Private inheritance is a way of implementing composition of classes. This C++ FAQ has a whole chapter about this topic, see http://www.parashift.com/c++-faq-lite/private-inheritance.html.
它不能解决问题,但它允许您更多的代码设计选项,在这种情况下,
Bar
中的所有方法在Foo
中都是私有的。It doesn't solve problems, but it allows you more code design options, in this case all methods from
Bar
are private inFoo
.