What are the cast operators of C ++? What is the difference and connection with the C-style type conversion (T) value?

Source: Internet
Author: User

Anyone who mentions static_cast, const_cast, reinterpret_cast, and dynamic_cast on the Internet will say that they have overcome the disadvantages of C-style type conversion and should use them.
But what are the disadvantages of the C style? What are the advantages of C ++ cast?
Before yesterday, I didn't even know what the cast was ). Yesterday, my colleagues mentioned this, so I did a little research. Some Experiment code is as follows:
 
1. Non-inherited type conversion
Class
{
 
};
 
Class B
{
Public:
Operator ()
{
Return ();
}
};
 
Int main ()
{
B B;
A a = (A) B; // execute operator ()
A a2 = static_cast <A> (B); // execute operator ()
A a3 = dynamic_cast <A> (B); // not allowed
A a4 = reinterpret_cast <A> (B); // not allowed
A a5 = const_cast <A> (B); // not allowed

Return 0;
}
 
2. const_cast
Struct
{
Int m;
 
A (int m = 0): m (m)
{
 
}
};
 
Int main ()
{
Const A;
 
A a2 = (A) a; // Yes, (A) Is there any same, a2 is A new variable
A2.m = 1; // a2 changes do not affect
 
A & a3 = (A &) a; // allow
A3.m = 2; // affects
// A & a4 = a; // No. The const limit takes effect.
A * pa5 = (A *) & a; // allows www.2cto.com
Pa5-> m = 3; // affects
// A * PA66 = & a; // No. The const limit takes effect.
 
// A aa2 = const_cast <A> (a); // not allowed
 
A & aa3 = const_cast <A &> (a); // allow
Aa3.m = 2; // affects
A * paa5 = const_cast <A *> (& a); // allow
Paa5-> m = 3; // affects
 
Const int I = 0;
Const int & i2 = I;
Const int * pi3 = & I;
// Int j = const_cast <int> (I); // not allowed
Int & j2 = const_cast <int &> (i2); // allow
Int * pj3 = const_cast <int *> (pi3); // allow
 
Return 0;
}
From the experiment at, with the help of external data, it seems that const_case only allows conversion between the same type with different cv delimiters.
It is worth noting that if type A is not A pointer or reference, you cannot use const_cast (it is meaningless if you use it, see A a2 = (a))
In cases where const_cast can be used, the (T) value form can be used, and the (T) value function completely overwrites const_cast.
 
2. reinterpret_cast
Class
{
Public:
Operator int *()
{
Return nullptr;
}
};
 
Int main ()
{
Int I = 0;
Double d = 1.0;
Int * p = nullptr;

// Int di = reinterpret_cast <int> (d); // not allowed
Int pi = reinterpret_cast <int> (p); // allow
// Int pi2 = static_cast <int> (p); // not allowed
// Double id = reinterpret_cast <double> (I); // not allowed
// Double pd = reinterpret_cast <double> (p); // not allowed
Int * ip = reinterpret_cast <int *> (I); // allow
// Int * ip2 = static_cast <int *> (I); // not allowed
// Int * dp = reinterpret_cast <int *> (d); // not allowed
 
A;
Int * pa = (int *) a; // allowed
Int * pa2 = static_cast <int *> (a); // allow
// Int * p2 = reinterpret_cast <int *> (a); // not allowed
 
Return 0;
}
It seems that reinterpret_cast can be considered as a conversion method between pointer and value. It does not involve any operator overload. Instead, it only converts the pointer to the literal value or converts the number to the pointer, the value of the conversion process has not changed, but only tells the compiler not to report Type mismatch.
In addition, if reinterpret_cast can be used, static_cast cannot be used unless the corresponding type conversion operator is defined.
When reinterpret_cast can be used, the (T) value method can also be fully qualified, and the (T) value function fully covers reinterpret_cast.
 
Dynamic_cast I think I still understand it, so I will not try it.
 
In summary, my understanding is as follows:
1. static_cast + const_cast + reinterpret_cast = (T) value
C ++ splits the three original C-style cast into three, and the three are orthogonal to each other. In most cases, static_cast replaces (T) value. It only replaces const_cast when the cv qualifier is removed. reinterpret_cast is used when the pointer literal value is obtained. The type conversion operator T () is executed by static_cast.
2. dynamic_cast is newly added to C ++. It is used for polymorphism. It only allows the conversion of pointer and reference of the type on the inheritance tree with polymorphism, and does not allow the conversion of the type itself. It does not appear for (T) values. There is no competition between the two, but it depends on different requirements.
(I do not know if this understanding is correct. Please criticize and correct it ~)
Is it easier to understand why new writing is promoted on the Internet for better refinement? Is there anything that (T) value cannot do but * _ cast can do? Or vice versa?


From stream comics
 

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.