Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
to the top
>
>
>
Explicit type casting

Explicit type casting

30 Oct 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

S'abonner

Comments (0)

close comment form
close form

Remplissez le formulaire ci‑dessous en 2 étapes simples :

Vos coordonnées :

Étape 1
Félicitations ! Voici votre code promo !

Type de licence souhaité :

Étape 2
Team license
Enterprise licence
** En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité
close form
Demandez des tarifs
Nouvelle licence
Renouvellement de licence
--Sélectionnez la devise--
USD
EUR
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
La licence PVS‑Studio gratuit pour les spécialistes Microsoft MVP
close form
Pour obtenir la licence de votre projet open source, s’il vous plait rempliez ce formulaire
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
I want to join the test
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
check circle
Votre message a été envoyé.

Nous vous répondrons à


Si l'e-mail n'apparaît pas dans votre boîte de réception, recherchez-le dans l'un des dossiers suivants:

  • Promotion
  • Notifications
  • Spam