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

How to quote the const of Category 11

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

Share

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

This article introduces the relevant knowledge of "how to quote the const of Clear11". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Const reference

In C++ language, references exist as an efficient and secure way to transfer data. In addition to normal reference types, const references can also be declared.

We have the following Image class.

Class Image

{

Public:

Image (int w, int h)

: width (w), height (h)

{

Data = new char [getSize ()]

}

Int getSize () {

Return width * height

}

Virtual ~ Image () {

If (data! = nullptr) {

Delete data

Data = nullptr

Width = 0

Height = 0

}

}

Private:

Int width = 0

Int height = 0

Char* data = nullptr

}

The above is just a prototype of this class, with only constructors, destructors, and the ability to get data sizes.

Next, add a function that compares whether the two Image are the same. The simplest form is roughly as follows.

Bool isSame (Image& img)

{

If (width = = img.width

& & height = = img.height) {

Return (memcmp (data,img.data,getSize ()) = = 0)

}

Else {

Return false

}

}

Reference type parameters are used here to avoid unnecessary copy actions. Of course, we can do better: since the comparison function does not need and should not modify the contents of the comparison object, we can also make a commitment in the following form:

Bool isSame (const Image& img)

{

If (width = = img.width

& & height = = img.height) {

Char* in = static_cast (img.data)

Return (memcmp (data,in,getSize ()) = = 0)

}

Else {

Return false

}

}

By adding a const modifier to the parameter, you can assure the caller of the isSame method that the contents of the img will not be modified.

Right value reference

Continue to add methods to merge part of one Image to another Image. The content of the function is roughly as follows (the details of the processing are ignored here):

Void merge (Image& img) {

/ / take over the data in img.

Img.height = 0

Img.width = 0

Img.data = nullptr

}

Similar operations are generally handled in two ways when inputting objects. Sometimes you want to refer to the input data without destroying it, and you can promise it by adding const modifiers to the parameters mentioned earlier; sometimes you want to take over the input data for efficiency or other reasons, like the state of the code above. The behavior at this time is more like data movement.

In the second way, if only a general reference type is defined, there is no way for the user to determine whether the operation will take over the data in the parameter through the method declaration. This uncertainty can cause a lot of trouble.

The solution to this problem is the title of today's article-right value reference. The code is as follows:

Void merge (Image&& img) {

/ / take over the data in img.

Img.height = 0

Img.width = 0

Img.data = nullptr

}

We declare the parameter as a right-value reference, requiring the data to be used capriciously like a temporary variable. The method to use this function is as follows:

Image img1 (100,100)

Image img2 (100,200)

Img1.merge (std::move (img2))

Notice the std::move in the code, which is the method provided in the standard library, which explicitly converts the left value to the right value reference type, thus telling the compiler that it can be treated like the right value (temporary variable). It also means that img2 is no longer used except for assigning or destroying it.

Clocking 11 provides a standard way to take over data by using right-value references.

This is the end of the introduction of "how to quote the const of Clear11". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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