Five Misconceptions About C++ That Need Reassessment

The Father of C++ Discusses
Five Misconceptions About C++
These five misconceptions have prevailed in C++ for many years:
1. “To understand C++, you must first learn C.”
2. “C++ is an object-oriented language.”
3. “Garbage collection is essential for reliable software.”
4. “To improve efficiency, you must write low-level code.”
5. “C++ is only useful for large, complex projects.”

If you still firmly believe in these misconceptions, this article can provide you with some reassessment. These views were correct for certain people and certain jobs at specific times. However, regarding today’s C++, with the widespread use of ISO C++11 standard compilers and tools, these views need reassessment.
Next, we will refute these misconceptions one by one.

1

“To understand C++, you must first learn C.”

This is incorrect. In fact, C++ is much easier to learn than C for foundational programming.
Although C can almost be considered a subset of C++, it is not the easiest to learn for beginners. C lacks markup support and type safety, and C++’s standard library is more user-friendly for simplifying simple tasks.
For example, for a very simple function that describes the format of an email address:
Five Misconceptions About C++ That Need Reassessment
It can be used like this:
Five Misconceptions About C++ That Need Reassessment
Whereas in C, explicit character manipulation and memory management are required:
Five Misconceptions About C++ That Need Reassessment
Then, it needs to be used like this:
Five Misconceptions About C++ That Need Reassessment
In comparison, which version is easier to learn? Which language is more efficient? Clearly, it is C++, as it does not require calculating parameter characters or allocating dynamic memory for short strings.
Regarding
the misconception of “learning C first” is not just a view held by a few individuals. The teachers who impart this typical view mainly have the following reasons:
  • Because they have rich experience in this area.

  • Because it is a course requirement.

  • Because it was how they learned when they were younger.

  • Because C is smaller than C++, making it easier to learn.

  • Because students will eventually have to learn C or the C subset of C++.

However, C is not the simplest and most useful subset of C++ to learn first. Learning C will be very easy once you know enough C++. This learning method can effectively reduce the cognitive and technical difficulties when transitioning from C to C++.
For modern C++ teaching methods, refer to the book: Programming: Principles and Practice Using C++. It even demonstrates how to learn to use C at the end of one chapter. This teaching method has been very successful among tens of thousands of students in several universities. Its second edition uses C++11 and C++14 to make learning easier.
The C++11 standard makes C++ more accessible to beginners. For example, here is a vector from the standard library initialized with a sequence of elements:
Five Misconceptions About C++ That Need Reassessment
In C++98, we could only initialize arrays and lists. In C++11, we can define a constructor that contains an initialization list with {} and any required type.
We can traverse the vector using a range-based for loop:
Five Misconceptions About C++ That Need Reassessment
Each element of v will call test() once.
The range of the for loop can traverse any sequence, so we can simplify the example by directly using an initialization list.
Five Misconceptions About C++ That Need Reassessment
The goal of C++11 is to make simple things simple. Simplifying code does not come at the cost of performance.
2

“C++ is an object-oriented language.”

This is incorrect. C++ supports both object-oriented and other programming styles; it is not limited to the narrow view of “object-oriented”. It supports a comprehensive programming technique that includes both object-oriented and generic programming. The best way to solve a problem often requires comparing multiple types. Best, in this context, refers to the shortest time, easiest to understand, most efficient, and most maintainable, etc.
The view that “C++ is an object-oriented language” leads people to consider using it only when they need to have many virtual (polymorphic) functions in a huge class hierarchy. However, this usage is inappropriate for many problems. This view may also lead others to criticize C++’s object orientation as impure. After all, if “good” is equated with “object-oriented”, then C++ also contains other non-object-oriented elements that are considered “bad”. These two perceptions generated by this view can lead people to give up learning C++. (The author’s point is that comparing C++ to a restaurant selling buns and rice noodles. If C++ is seen as a bun shop, it can lead to two misunderstandings: first, passersby may think that only buns are sold, not other items; second, bun lovers may think that if the bun shop also sells rice noodles, the buns must not be professionally made.)
For example:
Five Misconceptions About C++ That Need Reassessment
Is it object-oriented? Of course, it heavily relies on a class hierarchy containing virtual functions. Is it generic programming? Certainly, it heavily relies on parameterized containers (vector) and generic functions for_each. Is it functional programming? To some extent, yes; it uses anonymous functions (constructed by []). So what is it? It is modern C++: C++11.
I used both for loops and the standard library algorithm for_each just to showcase its features. In actual code, I would only use one of the loops.
Generic
Programming
Do you want to make the above code more generic? After all, it only applies to Shape base classes of vector pointers. What about lists and built-in arrays? What about “smart pointers” like shared_ptr and unique_ptr (resource management pointers)? Can objects that do not call the Shape class use draw() and rotate()? You can do it like this:
Five Misconceptions About C++ That Need Reassessment
You can use this program to traverse any sequence from start to end. This is a C++ style standard library algorithm. I used auto to avoid having to name the interface type for “objects like Shape class”. This is a feature of C++11, meaning “use the type of the expression used for initialization”. Hence, the type of p in the for loop determines what type of object it is. This use of auto to indicate the type of the anonymous function parameter is a new feature widely used in C++14.
As shown below:
Five Misconceptions About C++ That Need Reassessment
Here I assume Blob is a graphic type containing operation functions draw() and rotate(), while Container is a container type. The standard library list (std::list) has member functions begin() and end() to help users traverse the sequence of elements. This is very good classic object-oriented programming. However, what if the container does not support the C++ standard concept of traversing half-open sequences [b:e)? What if the library does not have member functions begin() and end()? Or, what if there is no container to traverse? For these situations, we can define independent begin() and end() with appropriate semantics. The standard library provides C-style arrays, so if the container is a C-style array, the problem is easily solved—C-style arrays are very common.
Now
let’s look at a more challenging example. Suppose the container retains pointers to objects and has a different model for access and traversal. For example, you might access a container like the one below:
Five Misconceptions About C++ That Need Reassessment
This style is not uncommon; we can map it to a sequence like [b,e):
Five Misconceptions About C++ That Need Reassessment
Note that this modification is inconsequential: I did not modify the container or the hierarchy of container classes supported by the C++ standard library for mapping containers to models for traversal. This is a form of rewriting rather than refactoring.
I chose this example to illustrate that these generic programming techniques are not limited to popular standard libraries. They also conform to common definitions of “object-oriented”, but they are not strictly object-oriented.
The view that C++ code must be object-oriented (meaning that every place uses hierarchy and virtual functions) deeply affects people’s evaluation of C++ performance. Some believe that when it comes to solving various types of runtime problems, only object-oriented is best. In the past, I thought so too. However, in fact, it also has a rigid side (for example, not all related types belong to the same hierarchy) and virtual functions cannot be inline functions (which leads to a lot of time wasted when handling many simple and important tasks).

How to Follow

① Copy “Xiāng Nóng Xìn Xī“, paste the search number in “Add Friend” to follow.

② Click the “+” in the upper right corner of WeChat, and select “Add Friends”, enter “Xiāng Nóng Xìn Xī” to find it.

③ If the article helps you, please click the button in the upper right corner, and select “Share to Moments to share the article to your Moments.

④ Scan the code for more excitement to come!

Five Misconceptions About C++ That Need Reassessment

Leave a Comment