|
1 | 1 | --- |
2 | | -description: "Learn more about: Casting in C++" |
3 | 2 | title: "Casting" |
| 3 | +description: "Learn more about: Casting in C++" |
4 | 4 | ms.date: 6/11/2024 |
5 | 5 | helpviewer_keywords: ["casting [C++]", "coercion [C++]", "virtual functions [C++], in derived classes [C++]", "static cast operator", "dynamic cast operator", "polymorphic classes [C++]", "classes [C++], polymorphism"] |
6 | 6 | ai. |
7 | 7 | --- |
8 | 8 | # Casting |
9 | 9 |
|
10 | | -The C++ language provides that if a class is derived from a base class containing virtual functions, a pointer to that base class type can be used to call virtual functions in the derived class object. A class containing virtual functions is sometimes called a "polymorphic class". |
| 10 | +The C++ language provides that if a class is derived from a base class containing virtual functions, a pointer to that base class type can be used to call virtual functions in the derived class object. A class containing virtual functions is sometimes called a "polymorphic class." |
11 | 11 |
|
12 | 12 | <br/> |
13 | 13 | Class hierarchy |
14 | 14 |
|
15 | 15 | An object of type `C` could be visualized as shown in the following figure. |
16 | 16 |
|
17 | 17 |  <br/> |
18 | | -Class C with sub-objects B and A |
| 18 | +Class C with subobjects B and A |
19 | 19 |
|
20 | | -Given an instance of class `C`, there is a `B` subobject and an `A` subobject. The instance of `C`, including the `A` and `B` subobjects, is the "complete object." |
| 20 | +Given an instance of class `C`, there's a `B` subobject and an `A` subobject. The instance of `C`, including the `A` and `B` subobjects, is the "complete object." |
21 | 21 |
|
22 | | -Since a derived class completely contains the definitions of all the base classes from which it is derived, it is safe to cast a pointer to any of the base classes (an upcast). Given a pointer to a base class, it may be safe to cast the pointer to an instance of a derived class (downcast). The actual object is said to be the "complete object." The pointer to the base class is said to point to a "subobject" of the complete object. For example, consider the class hierarchy shown in the following figure. |
| 22 | +Since a derived class completely contains the definitions of all the base classes from which it's derived, it's safe to cast a pointer to any of the base classes (an upcast). Given a pointer to a base class, it may be safe to cast the pointer to an instance of a derived class (downcast). The actual object is said to be the "complete object." The pointer to the base class is said to point to a "subobject" of the complete object. For example, consider the class hierarchy shown in the following figure. |
23 | 23 |
|
24 | | -Using run-time type information, it is possible to check whether a pointer actually points to a complete object and can be safely cast to point to another object in its hierarchy. Use the [dynamic_cast](../cpp/dynamic-cast-operator.md) operator to make safe casts. It performs the run-time check necessary to ensure that the operation is safe. |
| 24 | +Using run-time type information, it's possible to check whether a pointer actually points to a complete object and can be safely cast to point to another object in its hierarchy. Use the [dynamic_cast](../cpp/dynamic-cast-operator.md) operator to make safe casts. It performs the run-time check necessary to ensure that the operation is safe. |
25 | 25 |
|
26 | | -For conversion of nonpolymorphic types, you can use the [static_cast](../cpp/static-cast-operator.md) operator (this topic explains the difference between static and dynamic casting conversions, and when it is appropriate to use each). |
| 26 | +For conversion of nonpolymorphic types, you can use the [static_cast](../cpp/static-cast-operator.md) operator (this topic explains the difference between static and dynamic casting conversions, and when it's appropriate to use each). |
27 | 27 |
|
28 | 28 | The following example demonstrates the use of the `dynamic_cast` and `static_cast` operators: |
29 | 29 |
|
|
0 commit comments