私有继承与公有继承

发布于 2024-08-18 05:41:50 字数 1256 浏览 4 评论 0原文

我有一个基于这个问题的问题

在 部分 http://www.parashift。 com/c%2B%2B-faq-lite/private-inheritance.html#faq-24.3 提到以下内容:

私有继承的合法、长期使用是当您想要构建一个使用 Wilma 类中的代码的 Fred 类,并且 Wilma 类中的代码需要调用新类 Fred 中的成员函数时。在这种情况下,Fred 调用 Wilma 中的非虚拟对象,而 Wilma 调用自身(通常是纯虚拟对象),这些都被 Fred 覆盖。如果用组合来做到这一点会困难得多。

但是,我想知道为什么使用公共继承不能达到相同的效果。 即下面的 C# 代码做了同样的事情..

class Wilma
{
    protected void FredCallsWilma() {
        Console.Write("Fred Calls Wilma ;");
        WilmaCallsFred();
    }

    protected virtual void WilmaCallsFred() {}
}

class Fred : Wilma
{
    public void barney(){
        Console.Write("barney;");
        FredCallsWilma();
    }

    protected override void WilmaCallsFred(){
        Console.Write("Wilma Calls Fred ;");
    }
}

class Program
{
    static void Main(string[] args){
        Fred f1 = new Fred();
        f1.barney();
    }
}

它打印

巴尼;弗雷德打电话给威尔玛;威尔玛打电话给弗雷德

那么,c++ faq lite 中引用的私有继承有何特别之处。用公共继承代替私有继承不就能很好地实现这个结果吗?

I have a question based on this question

In the section
http://www.parashift.com/c%2B%2B-faq-lite/private-inheritance.html#faq-24.3
the following is mentioned:

A legitimate, long-term use for private inheritance is when you want to build a class Fred that uses code in a class Wilma, and the code from class Wilma needs to invoke member functions from your new class, Fred. In this case, Fred calls non-virtuals in Wilma, and Wilma calls (usually pure virtuals) in itself, which are overridden by Fred. This would be much harder to do with composition.

However, I would like to know why using public inheritence would not achieve the same effect.
i.e the following piece of c# code does the same thing..

class Wilma
{
    protected void FredCallsWilma() {
        Console.Write("Fred Calls Wilma ;");
        WilmaCallsFred();
    }

    protected virtual void WilmaCallsFred() {}
}

class Fred : Wilma
{
    public void barney(){
        Console.Write("barney;");
        FredCallsWilma();
    }

    protected override void WilmaCallsFred(){
        Console.Write("Wilma Calls Fred ;");
    }
}

class Program
{
    static void Main(string[] args){
        Fred f1 = new Fred();
        f1.barney();
    }
}

It prints

barney; Fred Calls Wilma ; Wilma Calls Fred

So what is special about private inheritence as quoted in the c++ faq lite. Would not substituting public inheritence for private inheritence work just fine to achieve that result?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

早乙女 2024-08-25 05:41:50

公共继承使得基类可以被外界访问。例如,指向派生类的指针可以(隐式地)转换为指向基类的指针。考虑从链表派生的堆栈。您通常不希望有人将您的数组隐式转换为其基本链接列表,这将允许他们执行堆栈不允许的操作,例如在中间插入或删除项目。

私有继承的通常含义是“根据...实现”——这意味着我们通常不想让外界/用户看到/知道/关心此类具有基类的事实。公共继承应遵循里氏替换规则,即派生类可以在任何需要公共基类的地方进行替换。对于私有继承,我们特别不说派生类可以替代基类。

公共继承是一种设计技术。其目的是允许您正在编写的代码被其他已经知道如何使用基类提供的接口的代码使用。

私有继承是一种实现技术。其目的只是通过使用基类中的代码来简化您的实现。

Public inheritance makes the base class accessible to the outside world. For example, a pointer to the derived class can be converted (implicitly, no less) to a pointer to the base class. Consider a stack that's derived from a linked list. You generally would NOT want somebody implicitly converting your array to its base linked list, which would allow them to do things not allowed for a stack, such as inserting or deleting items in the middle.

The usual meaning attributed to private inheritance is "Is implemented in terms of" -- which means we normally do NOT want to allow the outside world/user to see/know/care about the fact that this class has a base class. Public inheritance should follow the Liskov substitution rule, which says that a derived class can be substituted anywhere a public base class is called for. With private inheritance, we're specifically NOT saying that the derived class can be substituted for the base class.

Public inheritance is a design technique. Its intent is to allow the code you're writing to be used by other code that already knows how to work with the interface presented by the base class.

Private inheritance is an implementation technique. Its intent is only to simplify your implementation by using code from the base class.

塔塔猫 2024-08-25 05:41:50

是的,你是对的。引用的段落仅引用子类可以访问继承类的私有成员,以及可能的用例。

有些人错误地认为您无法访问继承类的私有成员。

Yes, you are correct. The quoted paragraph is only quoting that child classes have access to private members of inhereted classes, and a possible use case for that.

Some people incorrectly assume that you can't access private members of inherited classes.

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