Inheritance is Anti-Pattern

Appendix: Fast forward to reality

 

This is the age of modern C++. Visual Studio 2022, and other wonders.  Also, now there is this MSVC “permissive” compiler switch, and there was this upheaval and debate around it. And the most feverish ones in that debate have been the same ones who abused the inheritance in them dark ages of C++.

They have been using templates but in the wrong way.

And they have appeared “clever” too. So what exactly is the problem they now have with modern C++? It is the introduction of a so-called “Two-phase name lookup“. Another very good explanation/solution is here. Please do not glance over; do visit those two links.

Just an important note from me on the permissive MSVC compiler switch. If I may.

You can use the /permissive- , cl.exe compiler switch to ask for standards-conforming compiler behaviour.

As simple as that. This is not a switch for the conformance to a particular C++ version. It is simply here to warn you if you are breaking modern C++  rules. Remove that hyphen and you can sort-of-a break them.

The little quiz for fun and profit

The followers of the 1990-ies mass religion of inheritance, now need to change their code to conform to modern C++ compilers.

Just because in their youthful and fast past, they used the “clever” (template) inheritance, each and every base class or call, now has to be changed to make it conform to modern C++ compilers. Please consider this extremely simple code:

Please focus as I do have the following questions:

  1. Why is a class Derived a “better design” vs Aggregated class?
  2. Why is a class Derived more resilient to change vs Aggregated?
  3. Derived knows about Base member x. Is that good or bad?

And so on. I am sure I could add lots more questions here.

For the conclusion here is one of Mr Stroustrup’s slides:

Stroustrup on inheritance
Stroustrup on inheritance: It has been “systematically overused and misused”

Yes, Bjarne has nailed it.