Promotion of C + + static_cast

Source: Internet
Author: User

---static_cast Static type conversions

---static: compile-time

---Type conversions: conversion between normal types, void* and normal type pointers, inter-pointer conversions with inheritance (richer than dynamic type conversions)

---normal type conversions:

int a = 10; int b = 3; Requirements: float c = A/b = 3.333

1.a Change to float type

2.

3.

---conversion between void* and normal type pointers:


And I know it now. float* this type, the printed result is estimated that you can't imagine.


---transition between pointers with inheritance


There's no dynamic_cast so strict here.

1.p1 position is paired with b* position, otherwise the compilation does not pass

2. Just a pointer to the parent class--A parent class--a subclass pointer or a pointer to a subclass--subclass--the pointer to the parent class is true

3.P1 and P2 must have a new, otherwise


4. Careful use, low portability

5. Can replace dynamic_cast at a certain time

--const_cast:const pointer conversion to non-const pointer

--reinterpret_cast: This can be said to be a lawless compiler, between pointers, numbers and types of random strong, cheat the compiler, but the call is wrong or wrong

Promotion of C + + static_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.