C + + transformation operator "1" static_cast and Const_cast

Source: Internet
Author: User

"C + + transformation operator"

Goto statement has always been regarded as the "untouchables" in the program design, the low-order transition action It has a spell, although it still can survive, but the old C transformation is not the only choice.

The syntax structure for legacy transformations:

With a pair of parentheses plus an object name (identifier), and the parentheses and object names can be used anywhere in C + +, we simply can't answer the most basic transformation-related questions "does this program have any transformational actions?" ”

<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 use purposes, you just need to master the use of the method is good.

In C + +, 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 export a floating-point value. With the C legacy transformation, you can do this:

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

Using the new C + + transformation method, it should be written like 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 has the same power as the C legacy transformation, as well as the same limitations. For example, you cannot use static_cast to transform a struct into an int, or a double to pointer, which is a task that the C legacy transformation action could not have been able to accomplish. Static_cast is not even able to remove the constant (constness) of an expression because there is a new transformation operator const_cast The post of the specialized division.


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

Note: If you use const_cast for purposes other than the above, 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 representing SW, and is a Const object update (&CSW);                        Error! You cannot pass a const specialwidget* to a function update (const_cast<specialwidget*> (&CSW) that requires specialwidget*);   Yes, the constants of the &CSW are removed, and because of this, CSW (also SW) can be changed in this function update ((specialwidget*) &CSW);        C Legacy Transformation mode, can be implemented, but widget *PW = new Specialwidget;update (PW);                          Error! , PW is the exact specialwidget* type required by widget* but update (). Update (const_cast<specialwidget*> (PW));    Error! Cons_cast can only be used to change the constants and the variation of the inheritance system for the downward transformation (cast down) action can not be manipulated.







C + + transformation operator "1" static_cast and Const_cast

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.