A class is a user-defined type where the programmer can specify the representation, the operations, and the interface. The C++ core guidelines have a lot of rules for user-defined types.
The guidelines start with general rules but also include special rules for constructors and destructors, class hierarchies, overloading of operators, and unions.
Before I write about the special rules that are way more interesting, here are the eight general rules.
- C.1: Organize related data into structures (
- C.2: Use
classif the class has an invariant; use
structif the data members can vary independently
- C.3: Represent the distinction between an interface and an implementation using a class
- C.4: Make a function a member-only if it needs direct access to the representation of a class
- C.5: Place helper functions in the same namespace as the class they support
- C.7: Don’t define a class or enum and declare a variable of its type in the same statement
- C.8: Use
structif any member is non-public
- C.9: Minimize exposure of members
I will only write as much to the general class rules to make their intention clear.
General rules for classes
If data is related, you should put it into a struct or class; therefore, the second function is straightforward to comprehend.
An invariant is a logical condition that a constructor typically establishes.
The class Date has the invariants y, m, and d. They are initialized and checked in the constructor. The data type Pair has no invariant; therefore, it is a struct.
Due to the invariance, the class is easier to use. This is precisely the aim of the following rule.
The public methods are, in this case, the interface of a class, and the private part is the implementation.
From a maintainability perspective, the implementations of the class Date can be changed without affecting the user.
If a function needs no access to the internals of the class, it should not be a member. Hence you get loose coupling, and a change in the internals of the class will not affect the function.
Such a helper function should be in the namespace of the class.
Thanks to argument-dependent lookup (ADL), the comparison in (1) will additionally look for the identity operator in the Chrono namespace.
I admit: defining a class and declaring a variable of its type in the same statement confuses me.
This is quite a valid and often-used convention. If a data type has private or protected members, make it a class.
This rule, also called data hiding, is one of the cornerstones of object-oriented class design. It means that you should think about two interfaces for your class. A public interface for the general use case and a protected interface for derived classes. The remaining members should be private.
I will continue with the more special rules. Here is an overview:
- C.concrete: Concrete types
- C.ctor: Constructors, assignments, and destructors
- C.con: Containers and other resource handles
- C.lambdas: Function objects and lambdas
- C.hier: Class hierarchies (OOP)
- C.over: Overloading and overloaded operators
- C.union: Unions
Let’s continue with the two rules to concrete types.
First of all, I have to write about concrete types and regular types.
A concrete type is “the simplest kind of a class”. It is often called a value type and is not part of a type hierarchy. Of course, an abstract type can not be concrete.
A regular type is a type that “behaves like an int” and has, therefore, to support copy and assignment, equality, and order. To be more formal. A regular type Regular supports the following operations.
- Copy and assignment
The built-in types are regular such as the container of the standard template library.
Use a concrete type if you have no use case for a class hierarchy. A concrete type is way easier to implement, smaller, and faster. You do have not to think about inheritance, virtuality, references, or pointers, including memory allocation and deallocation. There will be no virtual dispatch and, therefore, no runtime overhead.
You have value.
Regular types (ints) are easier to understand. They are, per see, intuitive. If you have a concrete type think about upgrading it to a regular type.
The next post will be about the lifetime of objects: create, copy, move, and destroy.
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,