C++ Learning Tips: The Only Truth Is Magic Can Defeat Magic!

Before discussing how to learn C++, I would like to briefly talk about what kind of language C++ is.

C++ is undoubtedly recognized as a relatively difficult language to get started with and master. C++ gives programmers a high degree of freedom while also encompassing almost all programming paradigms.

This allows programmers to freely manipulate computer memory, embed assembly in code, design complex inheritance relationships, and control the compiler to perform certain calculations~~(you can even control the compilation state to achieve some kind of magic)~~

C++ Learning Tips: The Only Truth Is Magic Can Defeat Magic!

This often requires C++ programmers to have a solid foundation in the language and a certain level of knowledge related to computer principles. Otherwise, when we delve deeper into C++, we will find that C++ constantly punishes our weak foundations and ignorance of the underlying principles. Therefore, during the learning process, the most important thing is to thoroughly understand the syntax you learn and the principles behind it.

Of course, excessively digging into language details may lead beginners to encounter too many concepts at the beginning, so a good way to test your understanding during the early learning phase is to determine whether you can explain these concepts to yourself. Even if your understanding is not complete, that’s okay; as you continue to learn C++, your previous misconceptions will inevitably be corrected.

1It’s Best to Choose a Small Learning Scope at the Beginning

Overall, C++ includes four main components:

  • The procedural part similar to C language

  • The object-oriented part centered around classes

  • The template part for compiler-oriented programming

  • The C++ standard library

Beginners often feel lost due to these rich features and may encounter bugs that they cannot solve, causing them to give up before even starting to learn C++…

C++ guarantees zero-cost abstraction, meaning that unused features do not incur overhead. This also means that if you do not use those advanced features, you can treat them as if they do not exist. Therefore, defining a small learning scope to start with C++ is a good learning strategy that allows you to achieve learning goals relatively easily while avoiding complex problems caused by using too much unfamiliar syntax.

If you have a foundation in C language, you can focus primarily on learning the object-oriented part of C++; if you are just starting with programming, I think it might be more appropriate to learn C language first. C++ and C are closely related, and the procedural part of C++ is basically consistent with C language. Starting with C can help you temporarily avoid the complexity of C++ while learning the procedural part of C++.

2Write Code

The most important thing in programming is to actually write code; don’t just talk about it. I don’t believe anyone can learn any language just by reading books. In a sense, I think writing code is much more important than reading books. An efficient learning process should involve first understanding the basic usage through reading and then immediately writing code.

When you encounter problems while coding or want to use other methods, refer back to the relevant content in the book. This approach will yield much better results. If you only read the book once, you will likely remain unclear about many usages.

Another point is that if you want to gradually improve your C++ skills, you must actively use the usages you are not familiar with and avoid staying in your comfort zone. In most people’s impression, C++ is an object-oriented programming language, but object-oriented refers to a programming design method, not a concept unique to C++. If you have a solid foundation in C language, you can also design object-oriented programs using C language, although it may be a bit cumbersome.

When learning the object-oriented part, the first thing to ensure is that you genuinely possess the object-oriented programming mindset. To achieve this, your thought process should primarily focus on how to use object-oriented techniques to organize a certain amount of code and to appreciate the benefits brought by object-oriented programming. This is the only way to truly understand object-oriented languages.

Since object-oriented programming is not a feature unique to C++, most high-level languages currently support it. Therefore, most of the concepts related to object-oriented programming are similar. Among the object-oriented features provided by C++, the most challenging to understand are likely virtual functions and multiple inheritance. The former may require more time to fully comprehend, as it is a frequently used feature; as for multiple inheritance, it is not a big deal if you do not understand it, as it is rarely used. If you have programming experience in other object-oriented languages, learning this part should not be too difficult.

Most C++ programmers stop at mastering object-oriented programming. Generally speaking, this level of mastery is sufficient for daily work.

However, if we observe the functional changes from C++03 to C++20, we will find that C++ has been continuously improving and strengthening template-related features, indicating that templates are an important component of C++. It is precisely templates that make C++ a difficult language to learn and master.

Before learning templates, we need to clarify an extremely important concept, which is compile-time and run-time. As the name suggests, compile-time refers to the time when the program is compiled, and run-time is the same.

What we generally understand as compilation is actually the process of converting our code into assembly code. However, C++ can do more during compilation, and most of these tasks are accomplished through templates.

It can be said that templates are the dividing line between compile-time and run-time in C++. When learning C++ templates, we must understand what is done at compile-time and what is completed at run-time, as this is crucial for learning templates.

3Learning Template-Related Knowledge

Next, I would like to emphasize what problems templates solve.

First, consider the situation where you need to design a function interface that is compatible with different types of data inputs, and these data types share the same processing method.

In this case, if you do not use templates, you would need to design a specialized overloaded function for each type, resulting in a lot of code duplication and reducing code maintainability. However, if you use templates, you can solve this problem once and for all. This usage is called template generic programming, which mainly addresses interface compatibility issues.

Of course, we may also encounter another situation. Suppose you need a variable to represent the length of an RGB image array with dimensions 600*400 (you can be 100% sure that the image size will not change in the present or future). You are likely to write `int length = 600*400*3` rather than `int length = 760000`. Both of these will produce the same assembly code.

This means that the calculation `600*400*3` is computed at compile-time, which can be considered an enlightenment idea of template metaprogramming. However, in metaprogramming, the problem is no longer a simple multiplication of a few integers; it may involve calculating the result of multiplying several matrices and may include judgment logic. It can be seen that metaprogramming mainly solves the problem of improving code flexibility and maintainability without sacrificing program runtime performance.

I hope the above examples help you understand the role of templates. The concepts related to templates are often quite difficult to grasp, so if you are not yet familiar with the object-oriented part, it is best to treat templates as if they do not exist or simply understand them as a special macro.

If you already have a certain understanding of C++, you can consider starting to learn template-related knowledge. One important aspect of mastering templates is familiarity with C++’s type system, as generic templates involve more operations on types. If you are not familiar with the type system, you may write many bugs and find it difficult to identify the root of the problem.

From a personal perspective, learning about templates will deepen your understanding of C++’s type system. Starting with templates, you will encounter many important C++ language concepts, which are quite different from learning the object-oriented part.

During the study of templates, those key concepts and key terms that frequently appear must be understood (these concepts and terms often come from the C++ standard itself). You need to research online or carefully study the examples in books, and practice more. If necessary, you should design some experimental programs to verify your understanding.

If you find this part particularly difficult to learn, it may be because your foundational knowledge is not solid, or you genuinely lack practical use cases. In this case, you should pause your template studies, reinforce your basics, or wait until you have practical scenarios to learn the template part.

4The C++ Standard Library

Finally, there’s the C++ Standard Library, which provides many useful tools that can help us reduce the time spent on “reinventing the wheel”.

Overall, the usage methods of the standard library are relatively easy to learn. When learning, focus primarily on containers, iterators, and the usage of generic algorithms. There is a wealth of information available online regarding this part, with detailed explanations of both usage and internal implementation principles, so I won’t elaborate further here.

5Learning C++ Through Open Source Code

Lastly, I want to discuss how to learn C++ through open source code. If you have previously searched for open source projects on GitHub, you may have noticed that most projects heavily utilize templates, and many are pure template libraries.

Therefore, for C++, I do not recommend looking at open source projects without a foundation in templates unless you have found an interesting project that does not use templates (generally, these projects are older, and the language standard is typically C++98/03). However, this does not mean that you can only learn from open source projects if you have a deep understanding of templates.

In fact, you can try reading open source projects as long as you know some basic syntax and features of templates. I particularly recommend some shorter template libraries that consist only of header files (if you do not want to see too many new features, it is best to choose libraries with a language standard of C++11). Generally, such projects are suitable for beginners, and reading them can provide a sense of accomplishment. Learning from open source projects will greatly enhance your understanding of C++, but choosing what suits you is the most important.

Leave a Comment