C++的protected继承
高洛峰
高洛峰 2017-04-17 11:20:16
0
3
671

保护继承(protected)

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

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

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

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

上面这个是不是有问题

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
PHPzhong

During prototyped inheritance, both public members and protected members of the base class can be accessed by subclasses of the derived class. There is indeed a problem with this.
The simplest way to verify is to practice.

伊谢尔伦

I don’t understand the meaning of such a paragraph. If it is a book, I suggest you read it in another book

The visibility of inheritance is reduced, that is, protected inheritance will turn public into protected, and protected and private will remain unchanged.
As long as the derived class of the base class does not inherit privately, the derived class of the derived class can also access the non-private properties of the base class

左手右手慢动作

I also want to ask this question

Part of the code of %%%C++ data structure

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>;
    };
    

The following error occurs when compiling with 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]

The class iterator inherits from const_iterator, but it seems that the protected content in const_iterator cannot be called in iterator?

This code is excerpted from "Data Structure and Algorithm Analysis C++ Description" third edition, written by Mark Allen Weiss.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!