Type conversion mechanism in C ++

Source: Internet
Author: User

Http://blog.csdn.net/tianmo2010/article/details/8706495

Type conversion mechanism: it can be divided into implicit type conversion and display type conversion. display type conversion is also called forced type conversion (CAST). There are four naming forced type conversion operators: static_cast, dynamic_cast, const_cast, and reinterpret_cast.

Implicit type conversion is common and often occurs in mixed type expressions. The simplest is Integer increase. For All Integer types smaller than int, including char, signed Char, unsigned char, short, and unsigned short if all possible values of this type can be included in int, they will be upgraded to int type. Otherwise, will be upgraded to unsigned Int. In expressions containing the short and INT types, short is converted to int. If the int type is sufficient to indicate all unsigned short types, the unsigned short type is converted to int. Otherwise, the short type is converted to unsigned.
Int. On 32-bit machines, long and INT are usually expressed by a single word length. Therefore, when the expression contains two types: Unsigned int and unsigned int, the operands should be converted to the unsigned long type.

1 static_cast

 

Usage: static_cast <type-ID> (expression)

This operator converts expression to the Type-ID type, but does not check the runtime type to ensure the conversion security. It has the following usage:

 

1. It is used to convert pointers or references between classes and subclasses in the class hierarchy. It is safe to perform upstream conversion (converting the pointer or reference of the subclass to the base class representation). When performing downstream conversion (converting the base class pointer or reference to the subclass representation, because there is no dynamic type check, it is not safe.

2. Used for conversion between basic data types. For example, convert int to Char and convert int to enum. The security of such conversions must also be ensured by developers.

3. convert a null pointer to a null pointer of the target type.

4. convert any type of expression to void type.

Note: static_cast cannot convert the const, volitale, or _ unaligned attribute of expression.

 

3.2 dynamic_cast

 

Usage: dynamic_cast <type-ID> (expression)

 

This operator converts expression to type-ID objects.Type-ID must be a class pointer, class reference, or void
*; If type-ID is a class pointer type, expression must also be a pointer. If type-ID is a reference, expression must also be a reference.

 

Dynamic_cast is mainly used for upstream and downstream conversions between classes, and can also be used for cross conversions between classes.

 

When performing upstream conversion between classes, dynamic_cast and static_cast have the same effect. During downstream conversion, dynamic_cast has the type check function, which is safer than static_cast.

 

[CPP]
View plaincopy
  1. Class B
  2. {
  3. Public:
  4. Int m_inum;
  5. Virtual void Foo ();
  6. };
  7. Class D: Public B
  8. {
  9. Public:
  10. Char * m_szname [100];
  11. };
  12. Void func (B * pb)
  13. {
  14. D * pd1 = static_cast <D *> (PB );
  15. D * Pd2 = dynamic_cast <D *> (PB );
  16. }

In the above Code segment, if PB points to a D-type object, pd1 and Pd2 are the same, and it is safe to execute any operations of the D-type on these two pointers; however, if Pb points to a B-type object, pd1 will be a pointer to this object, and operations of Type D on it will be insecure (for example, accessing m_szname ), while Pd2 will beNull Pointer. Note: B must have virtual functions; otherwise, compilation errors may occur. static_cast does not have this restriction. This is because the runtime type check requires runtime type information, which is stored in the virtual function table of the class (the concept of the virtual function table can be seen in detail <inside
In C ++ object model>), only classes that define virtual functions have virtual function tables. classes that do not define virtual functions do not have virtual function tables.

 

In addition, dynamic_cast also supports cross cast ). The following code is used.

 

[CPP]
View plaincopy
  1. Class
  2. {
  3. Public:
  4. Int m_inum;
  5. Virtual void F (){}
  6. };
  7. Class B: public
  8. {
  9. };
  10. Class D: public
  11. {
  12. };
  13. Void Foo ()
  14. {
  15. B * pb = new B;
  16. Pb-> m_inum = 100;
  17. D * pd1 = static_cast <D *> (PB); // copile Error
  18. D * Pd2 = dynamic_cast <D *> (PB); // Pd2 is null
  19. Delete Pb;
  20. }

In function Foo, using static_cast for conversion is not allowed, and errors will occur during compilation. Using dynamic_cast for conversion is allowed, and the result is a null pointer.

3.3 reinpreter_cast

 

Usage: reinpreter_cast <type-ID> (expression)

 

Type-ID must be a pointer, reference, arithmetic type, function pointer, or member pointer.It can convert a pointer to an integer or an integer to a pointer.(First, convert a pointer to an integer. You can also obtain the original pointer value after converting the integer to the original type ).

 

This operator is used in many ways.

 

3.4 const_cast 

Usage: const_cast <type_id> (expression)

This operator is used to modify the const or volatile attributes of the type. In addition to const or volatile modification, type_id and expression are of the same type.

Constant pointers are converted to non-constant pointers and still point to the original objects. Constant references are converted to non-constant references and still point to the original objects; constant objects are converted to very large objects.

 

Voiatile and const classes. Take the following example:

[CPP]
View plaincopy
  1. Class B
  2. {
  3. Public:
  4. Int m_inum;
  5. }
  6. Void Foo ()
  7. {
  8. Const B B1;
  9. B1.m _ inum = 100; // comile Error
  10. B b2 = const_cast <B> (B1 );
  11. B2. m_inum = 200; // fine
  12. }

The above code will report an error during compilation, because B1 is a constant object and cannot be changed. Use const_cast to convert it into a very large object, you can change its data members at will. Note: B1 and B2 are two different objects.

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.