C + + transformation operator "1" static_cast and const_cast__c++

Source: Internet
Author: User

"C + + transformation operator"

The goto statement has always been regarded as the "untouchable" in the programming, the lower-order transformation action It has a fight, although it still can survive, however, the old mode of C transformation is not the only choice.

The grammatical structure of the old transformation:

Consists of a pair of parentheses plus an object name (identifier), and the parentheses and object names can be used anywhere in C + +, so we simply cannot answer the most basic transition-related questions. ”

<span style= "FONT-SIZE:18PX;" > (type) expression</span>

As a result,4 new transformation Operators (cast operators) are imported into C + +:static_cast, Const_cast, dynamic_cast, and Reinterpret_cast. For most purposes of use, you only have to master the use of the method.

In C + + mode, the above code will be changed to:

static_cast<type> (expression)

For example, suppose you want to transform an int into a double to force an integer expression to derive a floating-point value. Using the C-legacy transformation, you can do this:

int firstnumber,secondnumber;
...
Double result = ((double) firstnumber)/secondnumber;

Adopt the new C + + transformation method, should write this:

Double result = static_cast<double> (firstnumber)/secondnumber;

This form is very easy to identify, both for humans and for tool programs.


1, static_cast basically have the same power as the C-legacy transformation, and the same restrictions. For example, you cannot use static_cast to transform a struct into an int, or to transform a double into a pointer; these are the tasks that the old transformation of C can't have done. Static_cast cannot even remove the constants of an expression (constness) because there is a new transformation operator const_cast the Special division.


2, const_cast is used to change the constant (constness) or the volatileness of the expression, using const_cast is the emphasis on the compiler, through this transformation operator, the only thing you intend to change is the constant or the variation of the property. This will be implemented by the compiler.

Note: If you use const_cast for any of the above purposes, then the transition action will be rejected.

For example:

#include <iostream>
#include <string>
using namespace std;

Class Widget {...}
Class Specialwidget:public widget{...};
void Update (Specialwidget *PSW);
specialwidget SW;                    SW is a non-constant object
const specialwidget& CSW = SW;       CSW is a reference that represents SW and is a const object

update (&CSW);                        Error. You cannot pass the const specialwidget* to the specialwidget* function

Update (const_cast<specialwidget*> (&CSW));   OK, the constants of the &CSW are removed, and because of this, CSW (also SW) can be changed in this function (


(specialwidget*) &CSW);        C Old Transformation mode, can realize operation, but


Widget *pw = new Specialwidget;
Update (PW);                          Error. , PW is the exact specialwidget* type that widget* but update () requires.

Update (const_cast<specialwidget*> (PW));    Error. Cons_cast can only be used to change the constants and the ease of transition, and the downward transformation of the inheritance system (cast down) action cannot be manipulated.







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.