In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "what is C++ private inheritance". In the operation of practical cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Private inheritance
Through private inheritance, we can implement a has-a relationship.
But as we said earlier, we can also implement has-a relationships by including objects, so which one should we use when coding?
According to C++ Primer, most programmers would prefer to use inclusion because it is easier to understand. Class declarations contain objects, which we can use directly in member functions, which is obviously more intuitive, while the use of inheritance is very abstract.
Second, when multiple base classes inherit, it may also cause some problems. For example, an independent base class that contains a method of the same name, or an independent base class that has a common ancestor, and so on. In addition, only one parent object can be used with private inheritance, while any number of objects can be used by inclusion.
However, private inheritance also has some unique advantages, such as the use of protected members of the base class in derived classes, but there is no way to use these protected members or methods if they are included. In addition, derived classes can redefine virtual functions, but not the way to include classes.
It can only be said that in general, if we want to implement the inclusion relationship of has-a, we should use the way of including class objects, but in some special scenarios, using inheritance may be a better choice.
two。 Protect inheritance
Protection inheritance is a variant of private inheritance, protecting inheritance from using the keyword protected when listing base classes:
Class Student: protected std::string, protected std::valarray {...}
When using protected inheritance, both the public and protected members of the base class become protected members of the derived class.
Like private inheritance, the interface of the base class is available in derived classes, but is not available outside the inheritance hierarchy. When we continue to derive third-generation classes from derived classes, the difference between private inheritance and protected inheritance comes out. When using private inheritance, the third generation class cannot use the interface of the base class because the public method of the base class becomes private in the derived class. With protected inheritance, the public methods of the base class become protected in the derived class, so the third generation derived class can still be used.
3. Redefine access using using
When using protection derivation or private derivation, the public member of the base class becomes a protected member or a private member.
Suppose that if you want to make the methods of the base class available outside the derived class, one of the methods is to develop a separate derived class method in which you call the methods of the base class. For example, suppose you want the Student class to use the sum method of the valarray class. One way to do this is to declare a sum method in Student, and then call the sum method of the valarray class in that method:
Double Student::sum () const {return std::valarray::sum ();}
Another way is to wrap the calling function in a call to another function, even with a using declaration to indicate that a derived class can use a specific base class member.
For example, if we want the Student class to be able to use valarray's sum and max methods, we can add a using declaration to the public part of student.h:
Class Student: private std::string, private std::valarray {public: using std::valarray::min; using std::valarray::max;...}
As a result, valarray::min and valarray::max become available, as if they were public methods of Student:
Cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.