Suppose there is a certain class_adapter like this:

template <typename T> class Sequence { std::vector<T> v; public: explicit Sequence(size_t n = 0) : v(n) {} void push(const T& i) { v.push_back(i); } T& operator [](const size_t i) { return v[i]; } bool empty() const { return v.empty(); } size_t size() const { return v.size(); } //... } 

The same can be expressed by closed (protected) inheritance. For example:

 template < typename T > class Alternative : private std::vector<T> { using Rep = std::vector<T>; public: explicit Alternative(size_t n = 0) : Rep(n) {} void push(const T& i) { Rep::push_back(i); } T& operator [](const size_t i) { return Rep::operator [](i); } bool empty() const { return Rep::empty(); } size_t size() const { return Rep::size(); } //... }; 

In what cases the closed inheritance is preferable to aggregation? .. (examples made up)

  • The adapter may not encapsulate the object itself, i.e. can act as a view, for example, as std::string_view in relation to std::string . - isnullxbh
  • @isnullxbh, right, but this is not the answer to my question - AR Hovsepyan
  • " In what cases is private inheritance preferable to a particular class " .. Do you mean aggregation of a particular class is preferable? - Bogdan
  • I did not try to answer your question with this proposal - I just tried to focus your attention on the fact that in the case of an adapter encapsulation is not required. Perhaps I do not claim. - isnullxbh
  • @Bogdan, an aggregate can be anything (an instance of any class). In my opinion, the essence of the question should have been clear .. Just in case I made a change - AR Hovsepyan

1 answer 1

Both options are quite rigid, in the sense that they fix the type of entity being wrapped at the time of compilation. In your case it will always be std::vector<T> . A more flexible option would be to use a pointer member to the base class (if there is any hierarchy at all, therefore for std::vector , and many other standard container classes, this is irrelevant, due to their non-virtual nature) with the possibility of changing the type "to fly "and relying on its virtual interface.

If we speak directly about the comparison of aggregation with (closed / protected) inheritance, then the difference here is basically the same as with open inheritance. For inheritance in the case of having access (friends / derived classes), you can use a pointer / reference to the base class and work with the derivative in the presence of suitable virtual functions. In the case of aggregation, this possibility will not be due to the absence of a hierarchy of inheritance.

  • I do not understand what is irrelevant. I could specify the type of unit in the template parameters, and this would be the flexible option you are talking about ... - AR Hovsepyan
  • @ARHovsepyan no matter what you put in the template parameters - it will be fixed at the time of compilation . You cannot change class behavior during program execution . - 伪位蔚蠂慰位蠀蟿
  • Sometimes it is not necessary. Just in case: minus not from me ... - AR Hovsepyan
  • Sometimes variable variables are not needed if the value is constant. - 伪位蔚蠂慰位蠀蟿
  • But the question is not about that, so we will not comment on this issue - AR Hovsepyan