什么是私有继承,它解决哪些问题?

发布于 2024-12-18 07:49:08 字数 308 浏览 2 评论 0原文

有人可以解释一下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秋凉 2024-12-25 07:49:08

私有继承模型“根据...实现”。含义与“has-a”类似。区别在于:

  1. 使用私有继承,您不需要编写包装器(适合懒惰的程序员)

  2. “has-a”允许您更好的控制,您可以仅公开接口的子集,或者根据需要更改方法名称。

  3. 私有继承使异常安全变得困难,请查看Exceptional C++以获取更多信息

  4. 当您确实需要私有继承时,想要使用基类的受保护成员。

  5. 有时在混合类中使用私有继承(有效的c++内存管理章节)

我个人更喜欢使用“has” -a" 出于一般目的,我在排除其他选项后立即使用私有继承。

The private inheritance models "is-implemented-in-terms-of". The meaning is similar to "has-a". The differences are:

  1. With private inheritance you don't need to write a wrapper (good for lazy programmers)

  2. "has-a" allows you better control, you can expose only a subset of the interface or change method names if you like.

  3. Private inheritance makes exception safety difficult, have a look at exceptional c++ for more information

  4. You really need private inheritance just when you want to use a protected members of your base class.

  5. 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.

﹂绝世的画 2024-12-25 07:49:08

要了解私有继承,您必须首先了解公共继承。

如果您有以下情况:

class Base
{
public:
  void SomeFunc();
};

class Pub : public class Base {};

这意味着允许以下情况:

Pub aPub;
Base &basePart = aPub;

您可以采用派生类并获取对基类的引用(或指针)。您还可以这样做:

Pub aPub;
aPub.SomeFunc();

Base 中公开的内容在 Pub 中公开。

公共继承意味着任何地方的每个人都可以访问驱动类的基类组件。

私有继承有点不同:

class Priv : private class Base {};

Priv aPriv;
Base &basePart = aPriv;

最后一行通常不起作用。私有继承意味着只有可以执行此操作的函数和类要么是Priv的直接成员,要么是Priv的友元。如果您在 Priv 成员或朋友之外编写此代码,它将无法编译

同样:

Priv aPriv;
aPriv.SomeFunc();

只有在 Priv 的成员或好友中才有效。

私有继承意味着只有派生类的成员或朋友可以访问基类组件。所以成员函数可以调用派生类的基类部分,但外界没有人可以。

当您公开从另一个类派生一个类时,您是在说派生类“是”基类。它正是其中的一种。当您从基类私有派生时,您就向外界隐藏了该派生。这意味着您正在使用基类中的实现来实现派生类,但其他人不需要知道它。派生类是“根据”基类“实现的”。

To understand private inheritance, you must first understand public inheritance.

If you have the following:

class Base
{
public:
  void SomeFunc();
};

class Pub : public class Base {};

This means that the following is allowed:

Pub aPub;
Base &basePart = aPub;

You can take the derived class and get a reference (or pointer) to a base class. You can also do this:

Pub aPub;
aPub.SomeFunc();

That which was public in Base is public in Pub.

Public inheritance means that everyone, everywhere can access the base class components of the drived class.

Private inheritance is a bit different:

class Priv : private class Base {};

Priv aPriv;
Base &basePart = aPriv;

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 of Priv. If you write this code outside of a Priv member or friend, it will fail to compile.

Similarly:

Priv aPriv;
aPriv.SomeFunc();

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.

执笏见 2024-12-25 07:49:08

私有继承是实现类组合的一种方式。此 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.

娇纵 2024-12-25 07:49:08

它不能解决问题,但它允许您更多的代码设计选项,在这种情况下,Bar 中的所有方法在 Foo 中都是私有的。

It doesn't solve problems, but it allows you more code design options, in this case all methods from Bar are private in Foo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文