I needed three posts to present the 20 rules for class hierarchies in the C++ core guidelines. Here are the seven remaining rules.
To get the great picture. These are all special rules for class hierarchies.
- C.126: An abstract class typically doesn’t need a constructor
- C.127: A class with a virtual function should have a virtual or protected destructor
- C.128: Virtual functions should specify exactly one of
- C.129: When designing a class hierarchy, distinguish between implementation inheritance and interface inheritance
- C.130: Redefine or prohibit copying for a base class; prefer a virtual
- C.131: Avoid trivial getters and setters
- C.132: Don’t make a function
- C.133: Avoid
- C.134: Ensure all non-
constdata members have the same access level
- C.135: Use multiple inheritance to represent multiple distinct interfaces
- C.136: Use multiple inheritance to represent the union of implementation attributes
- C.137: Use
virtualbases to avoid overly general base classes
- C.138: Create an overload set for a derived class and its bases with
- C.139: Use
- C.140: Do not provide different default arguments for a virtual function and an overrider
The previous rule C.133, stated that you should avoid protected data. This means all your non-const data members should be either public or private. An object can have data members that do not prescribe the object’s invariants. Non-const data members that do not prescribe the invariants of an object should be public. In contrast, non-const private data members are used for the object invariants. To remind you: a data member having an invariant cannot have all values of the underlying type.
If you think about class design more generally, you will recognize two kinds of classes.
- All public: classes with only public data members because the data members have no invariant. Honestly, you should use a struct.
- All private: classes with only private or const data members that established the invariant.
Based on this observation, all your non-const data members should be public or private.
Imagine if you have a class with public and non-constant invariants. This means that you must maintain the data members’ invariance through the whole class hierarchy. This is quite error-prone because you can not easily control the invariants of your class. Or to say it differently. You break encapsulation.
It is a good idea that your interfaces will only support one aspect of your design. What does that mean? A concrete class must implement all functions if you provide a pure interface consisting only of pure virtual functions. In particular, in the case of a too-rich interface, the class has to implement functions it doesn’t need or make no sense.
Examples of two distinct interfaces are istream and ostream from the input and output streams library.
By combining both interfaces istream for input operations and ostream for output operations, we can quite easily create a new interface.
C.136: Use multiple inheritance to represent the union of implementation attributes, C.137: Use
virtual bases to avoid overly general base classes
Both rules are pretty special. Therefore I will skip them. The guidelines said that C.137 is relatively seldom used and that C.138 is similar to C. 129: When designing a class hierarchy, distinguish between implementation inheritance and interface inheritance.
This rule is quite apparent and holds for virtual and non-virtual functions. If you don’t use the using-declaration, member functions in the derived class hide the entire overload set. Sometimes this process is called shadowing. Breaking these rules is often quite confusing.
An example from the guidelines makes this rule quite clear.
Look at the last line. d.f(2.3) with a double argument is called, but the int overload of class D is used; therefore, a narrowing conversion from double to int happens. That is, most of the time, not the behavior you want. To use the double overload of class B, you must introduce it in the scope of D.
final is a new feature with C++11. You can use it for a class or a virtual function.
- If you derive a class My_widget final from a class Widget, you cannot further derive a class from My_widget.
- You can declare a virtual function as final. That means you can not override the function in derived classes.
If you use final, you seal your class hierarchy on a class base or a virtual function base. Often that has consequences you can not oversee. The potential performance benefit of using final should be your second thought.
Not following this rule can confuse. Have a look.
Here is the quite surprising output of the program.
What’s happening? Both objects b and d call the same function because the function is virtual and, therefore, late binding happens. This will not hold for the data such as the default arguments. They are statically bound, and early binding happens.
Now we are done with the design of class hierarchies. The question remains: who can we access the objects in the class hierarchy? Of course, I will answer this question in the next post.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, and Marco Parri Empoli.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,