Understanding Concrete Types in C++
A concrete type allows direct manipulation of its objects.
The Question Behind Every Type Choice
When programmers choose a type for their data, one important question is how the program will be able to work with objects of that type. Some types only let a program reach their objects indirectly, through a pointer or a reference. Others let the program work with the objects themselves. A concrete type is a type that allows direct manipulation of its objects.
What do you think happens?
Before reading on, think about it: what could a program do with a concrete object that it could not do if it could only reach that object through a pointer or a reference?
Reveal answer
The source names four advantages: the object can be placed in several kinds of storage, it can be referred to directly, it can be initialized fully and immediately, and it can be copied. Keep this question in mind as you read each advantage.
The question is useful because each advantage answers a different part of it. Placement answers where the object can live. Direct reference answers how the object can be reached. Initialization and copying answer how the object comes into existence and how new objects can be made from it.
What a Concrete Type Is
A concrete type is a type that allows direct manipulation of its objects. The source also describes it as the simplest kind of class.
The word simplest is the key to the definition. A concrete type is not defined by a special feature that sets it apart from other classes. Instead, it is defined by sitting at the simple end of the range of classes you could write. When you compare it with a more complicated class, the concrete type asks less of the reader and of the program.
The advantages of concrete types describe what becomes possible once a type is concrete. Objects can be placed in particular locations in memory, they can be referred to directly, they can be initialized fully and immediately, and they can be copied. The sections below take these one at a time.
Where a Concrete Object Can Live
The first advantage is placement. Objects of a concrete type can be placed in several kinds of storage. They can be placed on the stack, they can be placed in statically allocated memory, and they can be placed inside other objects. Because a concrete type supports all three placements, the programmer is not forced to store every object in one fixed kind of location.
Read the diagram as three different answers to one question: where can the object be stored? The concrete object is not confined to one storage location. The same type can be an automatic local object on the stack, a statically allocated object, or a part of a larger object.
Direct Reference Compared With Indirect Access
The second advantage is direct reference. The source is careful to note that this is different from referring to objects only through pointers or references. A pointer or a reference is one way to reach an object. A concrete type makes it possible to refer to the object itself, so the indirect route is not the only option.
- Counter c(5) names the object itself, so the program can use it directly with the dot operator.
- Counter* p holds the address of c, so the program reaches the same object through a pointer and the arrow operator.
- Both calls use the same object; the difference is the route taken to reach it.
Immediate and Complete Initialization
The third advantage is that concrete types allow objects to be initialized immediately and completely. The source gives constructors as the example mechanism for this. A constructor is the part of an object's creation that sets it up. With a concrete type, the object is brought into a usable, fully set-up state as part of its creation, rather than in a separate, later step.
- The constructor runs as part of declaring c, so the object is complete at its declaration.
- There is no separate setup call after the declaration and before the first use.
The benefit comes from the word immediately. The object does not have to be left in an incomplete state while the program waits for a later setup step. If you read the progression as a sequence, the object starts as something being created, the constructor takes part in setting it up, and the result is an object that is initialized immediately and completely.
Copying Creates a Second Object
The fourth advantage is that concrete types enable object copying. Copying means producing another object from an existing one. The source states this advantage briefly, but it is one reason concrete types are useful. A program can make a new object from an existing object of a concrete type, rather than being restricted to using only the original.
Before the copy there is one object. After the copy there are two objects, and the new one is the one that was created. The copy is a new object made from the existing one, so the program now has a second object it can work with on its own terms. The diagram marks the copy as the changed element, because it is the new object that the program produced.
Choosing a Concrete Type Over the Alternatives
The source gives a clear preference order. Where it is applicable, prefer a concrete type over more complicated classes and over plain data structures. When you shape a piece of a program, there are three options to weigh: a concrete type, a more complicated class, and a plain data structure. The concrete type is the one the source asks you to prefer when it fits.
| Option | What the source says about it |
|---|---|
| Concrete type | Preferred where applicable; the simplest kind of class; allows direct manipulation of its objects |
| More complicated class | The source says to prefer a concrete type over it where applicable |
| Plain data structure | The source says to prefer a concrete type over it where applicable |
The three options a designer weighs, and the source's guidance on each
The phrase where applicable matters. The source does not say the concrete type is always right. It says to prefer it when it applies, which means you first have to judge whether the concept is simple enough for a concrete type to fit.
When Concrete Classes Are Recommended
The second guideline names the two main situations for concrete classes. The first is representing simple concepts. A simple idea deserves a simple representation, and a concrete type keeps the representation from growing beyond what the idea needs. The second is building performance-critical components, the parts of a program where speed matters. The source recommends concrete classes there as well.
- Represent a simple concept: use a concrete type so the representation does not exceed what the idea needs.
- Build a performance-critical component: the source recommends concrete classes for these parts of a program.
- Before choosing, judge whether the concept is simple, because the guidance applies only where it is applicable.
Mistakes Beginners Make With Concrete Types
Common mistake Treating a concrete type as always the right choice
Why it failsThe source says to prefer a concrete type where it is applicable. It does not say the concrete type is always right.
Do this insteadJudge first whether the concept is simple enough for a concrete type to fit, then prefer it when it does.
Common mistake Assuming the object can only be reached through a pointer or reference
Why it failsDirect reference is one of the advantages of a concrete type. The object itself can be referred to directly.
Do this insteadUse the object by name when direct access is what the design needs, and reserve pointers or references for the cases that call for them.
Common mistake Thinking a copy is the same object as the original
Why it failsCopying means producing another object from an existing one, so the program ends up with two objects.
Do this insteadTreat the copy as a separate object and reason about each one on its own.
Common mistake Assuming a concrete object must live only on the stack, when the source lists static memory and enclosing objects as placements too
Practice Deciding on a Type
A program needs a small value representing a single measurement, and it will be read and updated in a tight loop. Using the source's guidance, would you choose a concrete type here, and which two of the four advantages matter most to the decision? Explain your reasoning in terms of the source's two situations.
Hints
- Ask whether the concept is simple, and whether the component is performance-critical.
- Think about which advantages describe how the object is placed, reached, and set up.
- The source names simple concepts and performance-critical components as the two main situations for concrete classes.
Key Takeaways
- A concrete type is a type that allows direct manipulation of its objects, and the source calls it the simplest kind of class.
- The four advantages are placement on the stack, in statically allocated memory, or inside other objects; direct reference; complete and immediate initialization, with constructors as the example mechanism; and copying.
- Prefer a concrete type over more complicated classes and plain data structures where it is applicable, after judging that the concept is simple.
- Concrete classes are recommended for simple concepts and performance-critical components.