Example details of the C++ type conversion operator

  • 2020-05-27 06:47:20
  • OfStack

Example details of the C++ type conversion operator

There are four type conversion operators in C++ to make the process more formal

dynamic_cast;
const_cast;
static_cast;
reinterpret_cast;

1. dynamic_cast

This operator was introduced in a previous article

https://www.ofstack.com/article/123252.htm

In summary, the syntax for the operator is as follows:


dynamic_cast < type-name> (expression)

If the transition fails, it returns 0, which is a null pointer.

The purpose of this operator is to make it possible to cast upward in the class hierarchy (such type conversions are safe because of the is-a relationship) without allowing other conversions.

2. const_cast

The const_cast operator is used to perform a type conversion for only one purpose, that is, to change the value to const or volatile, with the same syntax as the dynamic_cast operator.


const_cast < type-name > (expression)

The above type conversion will fail if other aspects of the type are also modified. That is, the types of type_name and expression must be the same, except that const or volatile characteristics (with or without) may differ

Also, const_cast is not a panacea. It can modify a pointer to a value, but the result of modifying the const value is uncertain.


using std::cout;
using std::endl;
void change(const int *pt, int n);

int main()
{
 int pop1 = 38383;
 const int pop2 = 2000;
 cout << "pop1,pop2: " << pop1 << " , " << pop2 << endl;
 change(&pop1, -103);
 change(&pop2, -103);
 cout << " pop1, pop2: " << pop1 << " , " << pop2 << endl;
 system("pause");
 return 0;
}
void change(const int *pt, int n) {
 int *pc;
 pc = const_cast<int *>(pt);
 *pc += n;
}

Operation results:


pop1,pop2: 38383 , 2000
 pop1, pop2: 38280 , 2000
 Please press any key to continue . . .

You can see that when change() is called, pop1 is modified, but pop2 is not. In zhchange (), an index is declared as const int *, so it cannot be used to modify pointing to int. The pc pointer removes the const feature, so it can be used to modify the value pointed to, but only if there are many const values executed. Therefore,pc can be used to modify pop1, not pop2.

3. static_cast

The syntax of the static_cast operator is the same as other type conversion operators


static_cast< type-name > (expression)

The above conversion is valid only if type-name can be implicitly converted to the type to which expression belongs or expression can be implicitly converted to the type to which type-name belongs, otherwise an error will occur.


High bar;
Low blow;
...
High *pb = static_cast<High *>( &blow) // legal 
Low *pl = static_cast< Low *> (&bar) // legal 
Pond *pmer = static_cast< Pond * > (& blow) ;// illegal 

4. reinterpret_cast

The reinterpret_cast operator is used for inherently dangerous type conversions.

Usage:


reinterpret_cast < type-name >(expression)

Example:


struct dat {short a;short b;} ;
long value = 0xA224B118;
dat * pd = reinterpret_cast <dat *> (&value);
cout <<hex <<pd->a ; // According to the former 2 A value of four bytes 

However, the reinterpret_cast operator does not support all type conversions. For example, a pointer type can be converted to an integer sufficient to store a pointer representation, but a pointer cannot be converted to a smaller integer or floating point. Another limitation is that you cannot convert a function pointer to a data pointer and vice versa.

If you have any questions, please leave a message or come to the site community to exchange discussion, thank you for reading, hope to help you, thank you for your support of the site!


Related articles: