C + + Notes

Source: Internet
Author: User

const_cast<type_id> (expression)

The operator is used to modify the const or volatile properties of a type. In addition to const or volatile adornments, the type_id and expression types are the same.
A constant pointer is converted into a very good pointer, and still points to the original object;
The constant reference is converted to a very literal reference and still points to the original object;

static_cast<type-id> (expression)

The operator converts expression to the Type-id type, but does not have run-time type checking to guarantee the security of the conversion. It is mainly used in the following ways:
① is used for the conversion of pointers or references between base classes (parent classes) and derived classes (subclasses) in a class hierarchy.
It is safe to make an upstream conversion (to convert a pointer or reference from a derived class to a base class representation).
When you make a downstream conversion (a base class pointer or reference is converted to a derived class representation), it is unsafe because there is no dynamic type checking.
② is used for conversions between basic data types, such as converting int to char, and converting int to enum. The security of this conversion is also to be ensured by the developer.
③ converts a null pointer to a null pointer of the target type.
④ converts any type of expression to a void type.
Note: static_cast cannot convert the const, volatile, or __unaligned properties of expression.

If the keyword const appears to the left of the asterisk *, it indicates that the object is a constant, and if it appears to the right of the asterisk, the pointer itself is a constant, and if it appears on both sides of the asterisk, both the finger and the pointer are constants.

Char greeting[]= "Hello";

char* p=greeting;

Const char* p=greeting; Const data

char* Const p=greeting; Const pointer

const char* Const p=greeting; const pointer, const data

Std::vector<int> VEC;

Const Std::vector<int>::iterator Iter=vec.begin (); ITER like a t* const pointer

Std::vector<int>::const_iterator Citer=vec.begin (); ITER like a const t* pointer

mutable

Member variables that are modified by mutable can always be changed, even within the const member function.

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.