Usage of class-type sequence operators of C ++ Lattice

Source: Internet
Author: User
Tags class operator types of functions

Usage of class-type sequence operators of C ++ Lattice

Think about the role-wise, class-type Broadcast Function (CAST), which is as cool as a goto phrase in programming. However, it is not intolerable because, in some cases, it is a necessity for a type-based economy.

However, the type-wise functions of the C-cell lattice do not represent all the type-wise functions.

As a result, they are too rough to allow you to perform operations within any type. However, if you want to perform a more precise class sequence, this will be a focal point. There are huge differences in these types of objects, such as pointing a pointer to a const object (pointer-to-const-object) define a pointer to a non-const object (pointer-to-non-const-object ), convert a base-type indicator to a sub-type indicator (that is, completely changing the object type ). The class operator of the C-cell of the traditional system does not divide the above two types of operators. (This is not surprising either, because the class-type operators of the C lattice are designed by C statements, instead of programming for C ++ ).

Second, the Class-type operator of the C-grams can recognize the difference in the program sentence. In terms of attention, type-based attention is composed of attention and cognition, which can be used anywhere in C ++. This makes it difficult to answer such a most basic type-sensitive question: "Does this program use a type-sensitive category? ". This is because the Manual Statement may ignore the sentence of the similar statement, and the tool program like grep cannot distinguish them from the sentence structure.

C ++ overcomes the limitations of C-cell class operator by introducing four new class operator operators. These four operators are static_cast, const_cast, dynamic_cast, and reinterpret_cast. In most cases, for these operators, you only need to know that the original operators are similar,
(Type) Expression

Now you should always look like this:
Static_cast <type> (expression)

For example, if you want to convert an int value to a double value, the result of floating point data is produced by the formula containing int type changes. If you use a C-cell type operator, you can perform the following operations:
Int firstnumber, secondnumber;
...
Double result = (double) firstnumber)/secondnumber;

If you use the new kind classification method described above, you should consider the following:
Double result = static_cast <double> (firstnumber)/secondnumber;

This kind of attention makes it easy to recognize humans or programs.

The function of static_cast is basically the same as that of the C-cell operator. It also has functional limitations. For example, you cannot use static_cast to convert struct to int type or double type struct to struct type like a C-cell struct, in addition, static_cast cannot remove const from the formula because another new class operator const_cast has this function.
Other new C ++ class operator operators are used where more restrictions are needed. Const_cast is used for class-type aggregate loss of the const or volatileness adequacy of the formula. By using const_cast, what you want to do is change the constness or volatileness adequacy of some things. This definition is bound by the parameter generator. If your category scheduler uses const_cast to modify constness or volatileness adequacy, your category scheduler will be rejected. The following are some examples:
Class widget {...};
Class specialwidget: Public widget {...};
Void Update (specialwidget * psw );
Specialwidget SW; // SW is a non-const object.
Const specialwidget & CSW = Sw; // CSW is a reference of SW.
// It is a const object
Update (& CSW); // please wait! Cannot parse a const specialwidget * variable
// Give a function to handle specialwidget * type changes
Update (const_cast <specialwidget *> (& CSW ));
// Confirm that the const of CSW is implicitly canceled (
// The CSW and SW change values are updated.
// The function can be updated)
Update (specialwidget *) & CSW );
// Same as above, but I used a better user
// Type of the C-cell operator
Widget * PW = new specialwidget;
Update (PW); // please wait! PW type is a widget *,
// Update function processing is specialwidget * type
Update (const_cast <specialwidget *> (PW ));
// Please wait! Const_cast role can be used in shadow
// Constness or volatileness .,
// It cannot be used to generate a class-type sequence into a subcategory.

So far, the most common use of const_cast is to abort the const attention of an object.

The second special type escape character is dynamic_cast, which is used to safely perform type-based implicit compression following the type of implicit correlation. That is to say, you can use dynamic_cast to refer to the base class or reference struct to point to its derived class or its sibling class, and you can know whether zookeeper is successful. The missing category will return a null index (when the specified type is modified) or an outbound period (when the referenced type is modified):
Widget * PW;
...
Update (dynamic_cast <specialwidget *> (PW ));
// Positive. Please specify a metric for the update function.
// Indicates the PW pointing to the specialwidget of the variant type.
// If PW actually points to an object,
// Otherwise, the expired domain names will be blank.
Void updateviaref (specialwidget & RSW );
Updateviaref (dynamic_cast <specialwidget &> (* PW ));
// Confirm. Upload messages to updateviaref
// Specialwidget PW refers to a volume, if PW
// It actually points to a specific object
// No, always output
Dynamic_casts imposes limits on the number of requests that can be processed by a stream worker. It cannot be used in a category that lacks function numbers (see the article M24), nor can it be used to disable constness:
Int firstnumber, secondnumber;
...
Double result = dynamic_cast <double> (firstnumber)/secondnumber;
// Please wait! No contract accepted
Const specialwidget SW;
...
Update (dynamic_cast <specialwidget *> (& SW ));
// Please wait! Dynamic_cast cannot upload bytes.
// Remove Const.

If you want to perform compaction in a type that does not have a commitment, you may think of static_cast. If you want to remove const, you always need to use const_cast.

The last of these four types of escape characters is reinterpret_cast. When this operator is used, the final result of this operator is implementation-defined ). Therefore, the replacement using reinterpret_casts is very portable.

The most common purpose of reinterpret_casts is to compile data between the function type. For example, assume that you have a function metric column:
Typedef void (* funcptr) (); // funcptr is a pointing function
// Indicates that the function does not contain a specific number.
// The return value type is void.
Funcptr funcptrarray [10]; // funcptrarray is
// The percentage column of 10 funcptrs

Let us pretend that you want (for some inexplicable reason) to store a pointer pointing to the following function in the funcptrarray comment column:
Int dosomething ();

You can't do this without having to go through the class operator directly, because the dosomething function has a class operator for the funcptrarray operator column. In the funcptrarray functions column, the return value of the function is void type, while that of the dosomething function is int type.
Funcptrarray [0] = & dosomething; // collect statistics! Type Mismatch
Reinterpret_cast allows you to force the scanner to view them in your way:
Funcptrarray [0] = // This compiles
Reinterpret_cast <funcptr> (& dosomething );

The replacement of the portable function index cannot be transplanted (C ++ does not guarantee that all function indicators are represented in the same way ), in some cases, such errors may lead to incorrect results (see the latest m31). Therefore, you should avoid using the portable function type, unless you are at the corner of the corner where your back-to-water is at war and your throat is at risk. A very profitable knife. A very profitable knife.

If your browser lacks support for the new method, you can replace static_cast, const_cast, and reinterpret_cast with the conventional method. We can also use the following macro to replace the New Class-type embedding method:
# Define static_cast (type, expr) (type) (expr ))
# Define const_cast (type, expr) (type) (expr ))
# Define reinterpret_cast (type, expr) (type) (expr ))

You can use it like this:
Double result = static_cast (double, firstnumber)/secondnumber;
Update (const_cast (specialwidget *, & SW ));
Funcptrarray [0] = reinterpret_cast (funcptr, & dosomething );

These modulo operators won't be as secure as real operators, but when your plug-in can support new classes, they can reflect the process of upgrading your generation.

There is no easy way to model the dynamic_cast operation. However, many function compute provides a function that securely performs a class-based operation between the derived class and the base class. If you do not have these types of functions and you have to perform such operations, you can also return to the class-type regression method of the C-cell lattice, however, in this case, you will not be able to determine whether the loss of a similar category will occur. Of course, you can also define the function of a macro to compare dynamic_cast, just like the functions of other classes:
# Define dynamic_cast (type, expr) (type) (expr)

Please remember, this module cannot fully implement the dynamic_cast function. It does not have the correct method to know whether the dynamic loading function is lost.

I know, yes, I know that the new class Operator is not very beautiful and it is also very troublesome to use the Operator. If you find that they seem to be quite confusing, C-cell type operators can still be used and valid. However, it is because of the lack of aesthetic feeling of the new type of escape character that it lacks in the content of precision and discrimination. Moreover, programs that use the new type of delimiters are more easily parsed (whether it is for manual or for tool programs ), they allow the attacker to generate a sequence that cannot be found. These are all powerful reasons for putting the C-cell type Attention method. There is also a third reason: it may be a good thing to make the class-type operators not beautiful and to break the trouble.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.