As we all know, C++ is a difficult language to learn and use. The difficulty is that even its founder, Bjarne Stroustrup, says he sometimes doesn’t understand the language.

Playing C++ for a long time has a feeling that it is always felt that this language is not finished, no matter how high your level, there must be black magic you do not know, the key is that you do not know you do not know.

C++ is a language that supports multi-paradigm programming, providing four complementary programming mindsets:

Object-based: indicates that it is based on objects

Object-oriented: Object oriented

Procedural -based: procedural

Generic Paradigm: Generic programming

You can write C-style code in C++, you can write Java-like object oriented code, and you can write the template metaprogramming version of the book. I think each paradigm is orthogonal, and you can just master C with Class without affecting your ability to write great software in C++ at all. A lot of time to see zhihu this kind of forum talk about C++ easy to be shut down, they like to use template yuan to show off skills, a lot of novice will look more than tianshu. How do you quickly determine if an element is in a collection? This is the solution I have seen on the Intranet, using C++ 11 variable-parameter templates and collapsed expressions:

template <typename T> is(const T& target) { return false; } template <typename T, typename... Args> inline bool IsContains(const T& target, const Args&... args) { return ((target == args) || ...) ; } // use IsContains(1, 2,3); // false IsContains("a", "a", "b", "c"); // trueCopy the code

We can accomplish the same thing with STD ::set, but with fewer set elements, it looks more “C++” and more showy. In C++, you can hardly find anything simple. For example, the most common code in Java:

public class Test { public void hello(String name) { System.out.println("hello " + name); }}Copy the code

In C++, beginners who have just read the syntax might write something like this:

class Test { public: void hello(std::string name) { std::cout << "hello " << name; }}Copy the code

This works, but it’s not the most efficient or normative. Why?

This involves the way C++ parameters are passed. For this type of writing, value passing requires the overhead of additional copy constructs. So you need to change the argument to a reference pass: void Hello (STD :: String & Name) and that’s it? NO! Since the hello function does not involve modifying the name variable internally, you need to modify the reference argument with const. On the one hand, we can explicitly tell the caller that the function does not modify the name variable. More importantly, if we do not add const, we will compile an error: Test Test; Const STD ::string name = “STD “; test.hello(name); Test.hello (” small north “) // Compile error

Since the argument is not const, taking a const variable as an argument causes compilation but. However, the usage scenario itself is reasonable, and the occurrence of such errors only shows that there is a problem with our class design. So you need to add const: and that’s it? NO! Because the Hello method is not decorated by const, it cannot be called by const Test. Such as:

const Test test; Test. Hello (" north "); // Compile errorCopy the code

But the Hello method itself does not change the object’s member variables. Const objects should be able to be called, so you need to add a const modifier to the hello method:

void hello(std::string &name) const {... }

It’s such a simple approach, and there are so many things you need to think about in C++ to make it work perfectly.

In terms of object generation construction, Java only needs to declare a constructor.

But in C++ there are copy constructs, copy assignments, move constructs, move assignments, destructors. For every class you have to think about whether it can be moved, how it can be moved, whether it can be copied, etc.

In C, there are only Pointers, while in Java there are only references. C++ has both Pointers and references, and references distinguish between lvalue references and rvalue references

And references to Pointers, references to Pointers, and so on, and then you combine that with the top const, the bottom const, and so on, and it’s not very comfortable. Writing C++ will give you a taste for performance and overhead reduction.

C++ is hard to learn, but for those of you who have no programming experience, just don’t delve into every arcane corner of the programming language. You can still learn C++ in 3 months by focusing on the most mainstream and core parts of C++.

Students who already have C, Java and other languages can master C++ faster. C++ advanced is the need to continue to learn in practice.

Every new version of C++ is mocked for being more and more complex, but every new feature is added to make C++ easier to use. For example, C++11’s range-for loop:

for (int& x : v) ++x;

V can be any container, and a C-style loop might look like this:

for (int i=0; i < MAX; i++) ++v[i];

However, there are potential errors such as overstepping boundaries, whereas range-for loops are more elegant and versatile. For the upcoming C++20, it will be a “big release” like C++11. There will be some new features introduced in C++ : Concepts Ranges Modules… C++ is so complex and difficult to learn that it retains such a high vitality due to its wide range of application scenarios, such as:

Desktop applications (Adobe PS, Chrome, Microsoft Office, etc.)

Background of large websites (such as wechat background, Google search engine, Sogou, etc.)

Games and game engine development (e.g. Unity)

Compilers (such as LLVM/Clang, GCC) interpreters (V8 engine, JVM, etc.)

Vision and intelligence engines (e.g. OpenCV, TensorFlow)

Another advantage of learning C++ well for storage, databases (e.g. MySQL and MongoDB, Rocksdb, etc.) is that you are no longer afraid to learn other languages, because many languages have syntax that can be found in C++ to some extent.

Finally, when you encounter problems, it is also very important to ask big gods. It is suggested to chat with this group and discuss with seniors. You will also get a lot of help. Can also exchange learning experience, technical problems, you can get PDF books source code, tutorials and so on for free use.