Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the core technical knowledge of C++

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what is the core technical knowledge of C++". In daily operation, I believe many people have doubts about the core technical knowledge of C++. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what is the core technical knowledge of C++?" Next, please follow the editor to study!

1. Automatic type inference auto & decltype ()

On types, many students may feel that this topic is relatively simple, because it is the basis of all languages. Statically typed languages such as Cpicket +, Java, etc., are basically explained in the first chapter, and many languages are common, but C++ has a powerful keyword auto, which I think needs to be emphasized. In fact, we can almost guess the function of the keyword from the meaning of the word-automatic type inference. That is, if the type of a variable is auto, it automatically deduces the type from the value of the variable. In that case, some friends may wonder: isn't it easy to see what type a variable is? It doesn't feel useful. Maybe in C or Java, indeed, the type of variable is clearly visible, but C++ is different. For example,

Templateauto add (T t, U u) {return t + u;}

There is a concept of template in C++. The template shown in the code implements the addition of two objects t and u. When we call the add function, due to the use of the auto keyword, the return value type is automatically derived, that is, the result type of Tbuttu, as follows:

Auto b = add (2,5); / / here b is int type auto c = add (2.3,8); / / here c is float type

At this point, some friends may say, it doesn't matter, I can still see the type of result at a glance, but here because of auto, it more reflects the advantages of C++ template, defining a function, you can carry out various types of data operation processing, and can return different types of results to achieve code reuse. So, in addition to this relatively simple situation, are there any more complex types? Let's go on to see:

Vector pred_boxes;for (auto pred_box = pred_boxes.begin (); pred_box! = pred_boxes.end (); pred_box++) {pred_box- > push_back (batch_id);}

This is a piece of code I intercepted from the caffe framework, regardless of its meaning, just focusing on the use of auto. Here is to implement a traversal function to store the batch_id in pred_box. We define an iterator pred_box, where we don't care about almost anything because auto automatically deduces its type. But what is the actual type of it? Please look at:

Vector::iterator pred_box

It is written in the above code, does it feel a lot more tedious? When it comes to traversal, we can also simplify the above code as follows:

Vector pred_boxes;for (auto pred_box: pred_boxes) {pred_box- > push_back (batch_id);}

The world is a little cleaner again!

After that, there is another keyword related to automatic type acquisition in auto,C++-decltype.

So what's the difference between the two? The auto keyword deduces its type from the value of the expression, and you can assume that it became this type for auto (it's not). In this process, the expression is evaluated, but sometimes we don't want the expression to be evaluated, but we still want to get the type of the expression value, so we use decltype, which is decltype (expression). Take a chestnut:

Decltype (pred_boxes) pred_boxes1

We define the same variable pred_boxes1 as the pred_boxes type

Again, for the following expression:

Int & k = 1 * type (k) j = 1

The type of j is the int reference type.

two。 Constant qualifier const and constexpr

After talking about automatic type inference, let's talk about the two keywords const and constexpr related to the definition of constants in C++.

First of all, they are qualifiers that qualify variables. When we do not want the value of a variable to be modified, we need to qualify with const. For example, we want to fix the size of a buffer:

Const int bufferSize = 1024

So, here bufferSize is an immutable variable, and the usage here is the same as in C, let's talk about the use of const in C++.

(1) the usage of const

Const is used with references

Const is often used with references in C++, as follows:

Const int i = 51210 Const int & j = I

Here I and j are constants, I is an integer constant, which is an integer reference constant (a reference to a constant), and it should be noted that the const constant can only be used as the value of the const constant (a bit of a mouthful! )

For example:

Where j is a reference to a constant, it can also be used as a value, initializing another constant as follows

Const int & k = j

Here k must be constant if it is a reference, but a variable if it is not a reference. As follows:

Int k = j

Some friends may have some doubts, since

Int & k = j

No, why just get rid of it? To explain briefly, a reference is not an entity object, but represents a binding relationship.

For

Int & k = j

In terms of, it means to bind k to j. Although the names of k and j are different, they occupy the same piece of memory in hardware. Since j is a constant, k must be a constant, and

Int k = j

It's different.

Const is used with pointers

Besides fooling around with references, const often likes to play with pointers. First of all, let's briefly talk about the difference between references and pointers. Although it is very simple, I think it is necessary to talk a little bit about it. As mentioned above, a reference is not an entity object, that is, unlike integer variables, structures, enumerations, and so on, these variables occupy memory space alone, and references represent a binding relationship. Bind an alias to a piece of memory space (so you must initialize when defining a reference variable). But the pointer is different. in essence, the pointer is a variable that stores the memory address, and it has a piece of memory space to store address information. So what's interesting about using const with pointers? Let's look at a few examples:

Const double pi = 3.14 Const double * ptr = π

Ptr is a pointer to type const double, so its value must be an address of type const double. Here, you only need to ensure that the value pointed to by the pointer remains unchanged. As for the value (address) stored in ptr, the value (address) stored in ptr can be changed, that is, the const here defines the value of the memory pointed to by the pointer, that is, the above pi is immutable.

As mentioned above, the pointer itself is also an entity object, unlike references with false names. So there's a guy named const pointer. Since it is called a constant pointer, const defines the pointer itself, which means that the value in the pointer is immutable, for example:

Double pi = 3.14 * const ptr = π

The value in ptr always points to & pi throughout its life cycle, and the pi can be changed at will, but the address stored in the ptr is fixed.

Then you can get it by combining the two.

Const double pi = 3.14 Const double * const ptr = π

In this way, neither the value that ptr points to nor the value itself can be changed.

Const is used with member functions of a class

In addition to being used with variables, const is also used with member functions of classes in C++. This involves class and object-related knowledge, as well as member functions, member variables, expansion will be more, and then put together with the class and object.

(2) the usage of constexpr

We talked about the use of const above, and let's briefly talk about constexpr. The meaning of constexpr as a qualifier is different from that of const. The purpose of constexpr is to let the compiler determine whether the value of a variable is a constant or a constant expression when initializing a variable. If the variable is qualified with constexpr, but the initialization value is not a constant or a constant expression, the compiler will report an error (because we are not sure whether the expression on the right is a constant expression when defining a constant) It seems that it is still difficult to explain, so here are a few examples:

Constexpr int a = k + 1 * Constexpr int b = newfun ()

In both cases, the compiler checks to see if KF1 and newfun () are constant expressions (functions) at compile time. If not, the above definition is not valid and the compilation will fail.

An important feature of a constant expression is that it has been determined during the compilation phase, so the following is not a constant expression:

Const int b = newfunc ()

Because, here, newfunc () only knows the result when the program is running, and the compiler does not check newfunc () at compile time because it is not qualified with constexpr.

We talked about pointers and references earlier, and here constexpr can also be used with them, but it is more restricted. The following examples are given with pointers:

Const int * I = nullptr;constexpr int * I = nullptr

The first I represents a pointer to the int constant

The second represents a constant pointer to int

3. Null pointer NULL and nullptr

Let's talk about the null pointer in C++. Friends who are familiar with C are very familiar with null pointers, NULL. Yes, there is still this keyword in C++. If you don't believe me, you can see the keyword table above. However, NILL is not recommended in Standard C++ because NULL is a macro definition of the integer 0. We often use null pointers to initialize a pointer variable. Imagine that it is always inappropriate to use an integer as the value of a pointer variable, although the compiler will not report an error. So the Craft 11 standard recommends that we use nullptr. The keyword itself represents a null pointer, which is more appropriate, so the next time you initialize a pointer variable in C++, use the real nullptr.

At this point, the study of "what is the core technical knowledge of C++" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report