Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
Explicit type casting

Explicit type casting

Oct 30 2010

C and C++ are languages that have weak static typing. Static means that types are known at compile time, and weak means that languages enable different types to be mixed in the same expression and perform implicit conversions.

Despite the presence of implicit conversions, many operations require explicit type casting. To perform explicit type casting, special programming language constructs are used. They specify how to handle the type of a particular variable or expression.

Let's look at the main types of type casts in C and C++.

const_cast

The const_cast operator is used in C++ to add or remove const and/or volatile qualifiers. Example:

const_cast<new_type>(expression)

static_cast

The static_cast operator is used in C++ for type conversion at compile time. If the conversion fails, a compilation error is issued. General view:

static_cast<new_type>(expression)

dynamic_cast

The dynamic_cast conversion operator is used in C++ for polymorphic type conversion at runtime.

Example:

dynamic_cast<new_type>(expression)

If conversion is impossible, two scenarios can occur:

  • if the resulting type is a pointer, the result of the expression is a null pointer;
  • if the resulting type is a reference, an exception of the std::bad_cast type is thrown.

reinterpret_cast

The reinterpret_cast operator is used in C++ to cast incompatible types based on their bit representation. For example, we can convert an integer to a pointer and vice versa. Example:

reinterpret_cast<new_type>(expression)

C-style cast

To cast an expression of any type to any other data type (with a few exceptions), use the C-style operator. Even though you can use it in C++, it is a bad practice since it becomes much easier to make a mistake. Example:

(new_type) expression

Sources

Popular related articles


Comments (0)

Next comments next comments
close comment form
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam