About the forced type conversion function in C ++

Source: Internet
Author: User

We all know that no matter what programming language we useForced type Conversion FunctionEach function can forcibly convert an expression to a specific data type. The following describesC ++Forced type conversion function in.

There are four types of forced conversion operators in Standard c ++:

  • Const_cast,
  • Reinterpret_cast,
  • Static_cast,
  • Dynamic_cast and so on.

1) static_cast <T *>)

Converts address a to type T. T and a must be pointer, reference, arithmetic, or enumeration.

Expression static_cast <T *> a). The value of a is converted to the type T specified in the template. During the running conversion process, no type check is performed to ensure the security of the conversion.

Example:

 
 
  1. Class B {...};
  2. Class D: public B {...};
  3. Void f (B * pb, D * pd)
  4. {
  5. D * pd2 = static_cast <D *> (pb); // insecure. pb may only be the pointer of B * PBS = static_cast <B *> (pd ); // safe
  6. ...
  7. }
  8. Class B {...};
  9. Class D: public B {...};
  10. Void f (B * pb, D * pd)
  11. {
  12. D * pd2 = static_cast <D *> (pb); // insecure. pb may be a pointer to B.
  13. B * PBS = static_cast <B *> (pd); // safe
  14. ...
  15. }
  16. Class B {...};
  17. Class D: public B {...};
  18. Void f (B * pb, D * pd)
  19. {
  20. D * pd2 = static_cast <D *> (pb); // insecure. pb may be a pointer to B.
  21. B * PBS = static_cast <B *> (pd); // safe
  22. ...
  23. }

2) dynamic_cast <T *>)

Completes the upgrade of the class hierarchy. T must be a pointer, reference, or non-type pointer. A must be a pointer or referenced expression.

Expression dynamic_cast <T *> a) converts a value to an object pointer of type T. If type T is not a base type of a, this operation returns a null pointer.

Example:

 
 
  1. Class {...};
  2. Class B {...};
  3. Void f ()
  4. {
  5. A * pa = new;
  6. B * pb = new B;
  7. Void * pv = dynamic_cast <A *> (pa );
  8. // Pv now points to an object of type
  9. ...
  10. Pv = dynamic_cast <B *> (pb );
  11. // Pv now points to an object of type B
  12. }

3) const_cast <T *>)

Remove constants from the type. Except for const or the number of unstable addresses, T and a must be of the same type.

Expression const_cast <T *> a) is used to remove the following attributes from a class: const, volatile, and _ unaligned.

Example:

 
 
  1. Class {...};
  2. Void f ()
  3. {
  4. Const A * pa = new A; // const object
  5. A * pb; // non-const object
  6. // Pb = pa; // an error occurs. You cannot assign a const object pointer to a non-const object.
  7. Pb = const_cast <A *> (pa); // now OK
  8. ...
  9. }
  10. Class {...};
  11. Void f ()
  12. {
  13. Const A * pa = new A; // const object
  14. A * pb; // non-const object
  15. // Pb = pa; // an error occurs. You cannot assign a const object pointer to a non-const object.
  16. Pb = const_cast <A *> (pa); // now OK
  17. ...
  18. }
  19. Class {...};
  20. Void f ()
  21. {
  22. Const A * pa = new A; // const object
  23. A * pb; // non-const object
  24. // Pb = pa; // an error occurs. You cannot assign a const object pointer to a non-const object.
  25. Pb = const_cast <A *> (pa); // now OK
  26. ...
  27. }

4) reinterpret_cast <T *>)

Any pointer can be converted to another type of pointer. T must be a pointer, reference, arithmetic type, pointer to a function, or pointer to a class member.

Expression reinterpret_cast <T *> a) can be used for conversion from char * to int *, or from One_class * To Unrelated_class *, which may be insecure.

Example:

 
 
  1. Class {...};
  2. Class B {...};
  3. Void f ()
  4. {
  5. A * pa = new;
  6. Void * pv = reinterpret_cast <A *> (pa );
  7. // Pv now points to an object of type B, which may be insecure
  8. ...
  9. }

However, there are also some weak types in C ++, so do not use forced type conversion. I hope this article will help you.

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.