How often is C ++ open language inheritance used?

Possible duplicate:
When should I use C ++ personal inheritance?

I wanted to make this community-wiki, but I don’t see the button ... can anyone add it?

I can’t come up with a single case that I got from a class in a non-public key, and I can’t remember that he is making code that does this.

I would like to hear real examples and samples where this is useful.

+7
source share
6 answers

Your mileage may vary ...

The tough answer will be that non-public inheritance is futile.

Personally, I use it in either of two cases:

  • I would like to initiate an empty base optimization, if possible (usually in the template code with predicates passed as parameters)
  • I would like to override the virtual function in the class

In both cases, I use private inheritance this way, because inheritance itself is an implementation detail.

I saw people using private inheritance more liberally and almost systematically instead of composition when writing wrappers or expanding behavior. C ++ does not provide a “simple” delegate syntax, so you can write using Base::method; to immediately provide a method instead of writing the correct call forwarding (and all its overloads). I would say that this is a bad form, although it saves time.

+6
source

If you have chosen inheritance for shell development, then inheritance is the way to go. You no longer need or need access to methods and members of the base class from outside your wrapper class.

 class B; class A { public: A(); void foo(B b); }; class BWrap; class AWrap : private A { public: AWrap(); void foo(BWrap b); }; //no longer want A::foo to be accessible by mistake here, so make it private 
+3
source

Sometimes inheriting classes that do not have any virtual functions and a virtual destructor (for example, STL containers), you may need non- public inheritance. eg.

 template<typename T> struct MyVector : private std::vector<T> { ... }; 

This will result in the ban of processing (pointer or link) of the database ( vector<> ) to obtain a derived class ( MyVector<> ):

 vector<int> *p = new MyVector<int>; // compiler error ... delete p; // undefined behavior: ~vector() is not 'virtual'! 

Since we get a compiler error in the first line, we will persist from undefined behavior in the next line.

+2
source

If you exit a class without a virtual destructor, then public inheritance means that users of the class can call delete on a pointer to the base, which leads to undefined behavior.
In such a scenario, it makes sense to use personal Inheritance.

The most common example of this is output from private STL containers that do not have virtual destructors.


C ++ FAQ has a great example of Private Inheritance, which extends to many real-time scenarios.

Valid long-term use for private inheritance is when you want to build a Fred class that uses code in the Wilma class, and code from the Wilma class must call member functions from your new Fred class. In this case, Fred calls non-virtual in Wilma, and Wilma itself (usually pure virtual), which are redefined by Fred. That would be a lot harder to do with the composition.

Code example:

 class Wilma { protected: void fredCallsWilma() { std::cout << "Wilma::fredCallsWilma()\n"; wilmaCallsFred(); } virtual void wilmaCallsFred() = 0; // A pure virtual function }; class Fred : private Wilma { public: void barney() { std::cout << "Fred::barney()\n"; Wilma::fredCallsWilma(); } protected: virtual void wilmaCallsFred() { std::cout << "Fred::wilmaCallsFred()\n"; } }; 
+2
source

Since private inheritance has the only known use of implementation inheritance, and since it can always be done using containment instead (which is less simple to use, but encapsulates relationships better), I would say that it is used too often.

(Since no one ever told me what protected inheritance means, let no one know what it is and pretend that it does not exist.)

+2
source

Non-public (almost always private) inheritance is used for inheritance (only), not an interface. I used it mainly, but not exclusively, in mixins.

For a good discussion on this topic, you can read Barton and Nackman (Scientific and Engineering C ++: An Introduction with Advanced Features) Methods and Examples, ISBN 0-201-53393-6. Despite the name, large parts of the book are applicable to all C ++, and not just to scientific and engineering Applications. And despite its date, it’s still worth reading.)

0
source

All Articles