简体   繁体   English

有没有我们需要* C ++中的受保护继承的例子?

[英]Are there any examples where we *need* protected inheritance in C++?

While I've seen rare cases where private inheritance was needed, I've never encountered a case where protected inheritance is needed. 虽然我已经看到需要私有继承的罕见情况,但我从未遇到过需要保护继承的情况。 Does someone have an example? 有人有例子吗?

People here seem to mistake Protected class inheritance and Protected methods. 这里的人似乎错误了Protected class继承和Protected方法。

FWIW, I've never seen anyone use protected class inheritance, and if I remember correctly I think Stroustrup even considered the "protected" level to be a mistake in c++. FWIW,我从未见过有人使用受保护的类继承,如果我没记错,我认为Stroustrup甚至认为“受保护”级别是c ++中的一个错误。 There's precious little you cannot do if you remove that protection level and only rely on public and private. 如果你删除了这个保护级别并且只依赖公共和私人,那么你就无法做到这一点。

There is a very rare use case of protected inheritance. 有一个非常罕见的受保护继承的用例。 It is where you want to make use of covariance : 这是您想要使用协方差的地方

struct base { 
    virtual ~base() {} 
    virtual base & getBase() = 0;
}; 

struct d1 : private /* protected */ base { 
    virtual base & getBase() { 
        return this; 
    } 
}; 

struct d2 : private /* protected */ d1 {
    virtual d1 & getBase () { 
        return this; 
    } 
}; 

The previous snippet tried to hide it's base class, and provide controlled visibility of bases and their functions, for whatever reason, by providing a "getBase" function. 之前的代码片段试图隐藏它的基类,并通过提供“getBase”函数,无论出于何种原因提供基本及其功能的可控可见性。

However, it will fail in struct d2 , since d2 does not know that d1 is derived from base . 但是,它会在struct d2失败,因为d2不知道d1是从base派生的。 Thus, covariance will not work. 因此, covariance行不通的。 A way out of this is deriving them protected, so that the inheritance is visible in d2. 解决这个问题的方法是将它们保护起来,以便继承在d2中可见。

A similar example of using this is when you derive from std::ostream , but don't want random people to write into your stream. 使用此类似的示例是从std::ostream派生,但不希望随机人员写入您的流。 You can provide a virtual getStream function that returns std::ostream& . 您可以提供一个返回std::ostream&的虚拟getStream函数。 That function could do some preparing of the stream for the next operation. 该函数可以为下一个操作做一些流准备。 For example putting certain manipulators in. 例如,放入某些操纵器。

std::ostream& d2::getStream() {
    this->width(10);
    return *this;
}

logger.getStream() << "we are padded";

C++ FAQ Lite mentions of a case where using private inheritance is a legitimate solution (See [24.3.] Which should I prefer: composition or private inheritance? ). C ++ FAQ Lite提到使用私有继承是一种合法的解决方案(见[24.3。]我应该选择哪种:组合或私有继承? )。 It's when you want to call the derived class from within a private base class through a virtual function (in this case derivedFunction() ): 当你想通过虚函数(在本例中为derivedFunction() )从私有基类中调用派生类时:

class SomeImplementationClass
{
protected:
    void service() {
        derivedFunction();
    }

    virtual void derivedFunction() = 0;      

    // virtual destructor etc
};

class Derived : private SomeImplementationClass
{
    void someFunction() {
        service();
    }

    virtual void derivedFunction() {
        // ...
    }

    // ...
};

Now if you want to derive from the class Derived, and you want to use Base::service() from within the derived class (say you want to move Derived::someFunction() to the derived class), the easiest way to accomplish this is to change the private inheritance of Base to protected inheritance. 现在,如果你想从Derived类派生,并且你想在派生类中使用Base::service() (比如你想将Derived::someFunction()移动到派生类),那么最简单的方法就是这是将Base的私有继承更改为受保护的继承。

Sorry, can't think of a more concrete example. 对不起,想不出更具体的例子。 Personally I like to make all inheritance public so as to avoid wasting time with "should I make inheritance relation protected or private" discussions. 就个人而言,我喜欢将所有继承公开,以避免浪费时间与“我应该保持继承关系保护或私人”讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM