Differences between the operators static_cast, const_cast, reinterpret_cast, and dynamic_cast

Source: Internet
Author: User

1 static_cast operator
: The static_cast operator can be applied wherever standard conversion operators are applicable.
Syntax:
valueoftargettype = static_cast (valueofsourcetype);
example:
double D; int I = 20;
d = static_cast (I);

also:
Class A
{< br> protected:
int m_x;
char * m_username;
public:
A (int x)
{< br> m_x = x;
}< br> A (char * username)
{< br> m_username = new char [strlen (username) + 1];
strcpy (m_username, username );
}< br> operater int ()
{< br> return m_x;
}< br> operator char *() const
{< br> return m_username;
}< BR >}

A (2);
int x = static_cast ();
char * P = static_cast (a);
note: there is only one reason for these static_cast operators to work normally: Class A supports overload of the int and const char * operators. otherwise, it is useless like using standard conversion operators. In general, static_cast is rarely used for pointer conversion. For example,
int * PX;
Double Y = 2.2;
PX = static_cast (& Y); // syntax error, can not convert double * to int *
// you can understand this. static_cast is generally used for conversion of different types of data, but not for conversion pointers. Reinterpret_cast, on the contrary, is only responsible for pointer conversion of different types.

reinterpret_cast operator
personally, reinterpret_cast is mainly used for conversion of different types of pointers.
int * PX;
Double Y = 2.2;
PX = reinterpret_cast (& Y); // syntax OK, but it's meanningful ?? No syntax error, but it does not make any sense
// * PX is a value
doule * py = reinterpret_cast (PX); // OK, * py = 2.2, so incredible!

const_cast operator
the const_cast operator can eliminate the constant feature of constant data or constant objects.
Syntax:
nonconstvalue = const_cast (constvalue)
the function is to remove the const feature of the source value constvalue, therefore, a constant value constavalue can be assigned to a nonconstvalue. nonconstvalue must be of typename type and constvalue must be of const typename type.
example:
const int x = 12;
int * PX = X; // syntax error, int pointer can not point to const int
can be changed to:
const int x = 12;
int * PX = const_cast (& X);
* PX = 20; // OK;
example:
class account
{< br> int balance;
Public:
account (int B)
{< br> balance = B;
}

Void operater + = (INT newbalance)
{
Balance + = newbalance;
}
}

Const account A (9000 );
Account * pA = & A; // syntax error!
* PA ++ = 1000; // syntax error!
You can change it:
Const account A (9000 );
Account * pA = const_cast <account *> (& );
* Pa + = 1000;
In this case, the balance of const object A is changed to 10000 !!

In general, one of the main functions of const_cast is to remove const protection. Therefore, it is better to use less.

4 dynamic_cast Operator
Dynamic_cast operator is a component of run_time_type information (rtti) supported by C ++. The dynamic_cast operator is used to convert the pointer (or reference) of a base class to the pointer (or reference) of a derived class)

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.