Heim  >  Fragen und Antworten  >  Hauptteil

C++的protected继承

保护继承(protected)

保护继承与私有继承相似,基类成员对其对象的可见性与一般类及其对象的可见性相同,public成员可见,其他成员不可见。

基类成员对派生类的可见性,对派生类来说,基类的public和protected成员是可见的:基类的public成员和protected成员都作为派生类的protected成员,并且不能被这个派生类的子类所访问;基类的private成员是不可见的:派生类不可访问基类中的private成员。

基类成员对派生类对象的可见性对派生类对象来说,基类的所有成员都是不可见的。

所以,在保护继承时,基类的成员也只能由直接派生类访问,而无法再向下继承。

上面这个是不是有问题

高洛峰高洛峰2715 Tage vor657

Antworte allen(3)Ich werde antworten

  • PHPz

    PHPz2017-04-17 11:22:16

    protoected继承时, 基类的public成员和protected成员都可以被派生类的子类访问。这个确实有问题。
    最简单的验证办法就是实践了。

    Antwort
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 11:22:16

    没看懂这么一段话绕的什么意思,如果是书,建议楼主换一本书看

    继承的可见性是变小的,也就是 protected继承,会将public变成protected,protected和private不变。
    基类的派生类只要不是private继承的,那么派生类的派生类也是可以访问到基类的非private属性的

    Antwort
    0
  • ringa_lee

    ringa_lee2017-04-17 11:22:16

    这个问题,我也想问一下

    %%%C++数据结构的部分代码

    class const_iterator

        {
            public:
                const_iterator() :current( NULL )
                { }            
                const Object & operator*() const
                {
                    return retrieve();
                }    
                const_iterator & operator++()
                {
                    current = current->next;
                    return *this;
                }    
                const_iterator operator++(int)
                {
                    const_iterator old =*this;
                    ++(*this);
                    return old;
                }            
                bool operator ==(const const_iterator &rhs) const
                {
                    return current == rhs.current;
                }
                bool operator != (const const_iterator &ths) const
                {
                    return !(*this == ths);
                }                
            protected:
                Node * current;
                **Object & retrieve() const**
                {
                    return current->data;
                }    
                const_iterator(Node *p): current(p)
                {  }    
                friend class List<Object>;
        };
        
        class iterator : public const_iterator
        {
            public:
                iterator( )
                { }
                Object & operator*()
                {
                    **return retrieve();**
                }
                const Object &operator* () const
                {
                    return const_iterator::operator*();
                }
                iterator & operator++()
                {
                    current = current->next;
                    return *this;
                }
                iterator operator++ (int)
                {
                    iterator old =*this;
                    ++(*this);
                    return old;
                }
            protected:
                iterator(Node *p) :const_iterator( p)
                    { }
                friend class List<Object>;
        };
        
    

    在利用dev-C++进行编译出现如下错误:

    In member function 'Object& List<Object>::iterator::operator*()':
    [Error] there are no arguments to '**retriev**e' that depend on a template parameter, so a declaration of 'retrieve' must be available [-fpermissive]
    [Error] there are no arguments to 'retrieve' that depend on a template parameter, so a declaration of 'retrieve' must be available [-fpermissive]
    

    类 iterator 继承自const_iterator ,但是在iterator中似乎并不能调用const_iterator中protected中的内容?

    这段代码 ,摘自《数据结构与算法分析C++描述》第三版,Mark Allen Weiss著。

    Antwort
    0
  • StornierenAntwort