You are on page 1of 12

C++ Type C

Casting
asting
C++ Type Casting

z Converting a value of a given type into


another
th type
t is
i known
known as type-casting.
ti
z Conversion can be implicit or explicit

2
C++ Implicit Conversion

z Implicit conversions do not require any


operator.
t
z They are automatically performed when a
value is copied to a compatible
compa type.

E.x.. 1. typeconversion.cpp
2. typeconclass.cpp

3
C++ Explicit Conversion

z Conversions that imply a different


interpretation of the value, require an explicit
conversion using type cast operator.
B b=(B) a; or B b=B (a);
z Using type cast operators indiscriminately on
classes and pointers to classes can lead to
code that while being syntactically correct
can cause runtime errors.
E.x.. 1.explicit_type_con_class.cpp
4
C++ Explicit Conversion

z In order to control types of conversions between


classes, there are four specific casting operators:
1. dynamic_cast,
2. reinterpret_cast,
ti t d lt
3. static_cast
4. const_cast.

5
C++ dynamic_cast

z dynamic_cast can be used only with


pointers and d references to objects.
i t
z Its purpose d tofensure that
is t the
bj result
t of the
type conversion is a valid
va complete object of
the requested class.
z dynamic_cast is always successful when we
cast a class to one of its base classes

Syntax: dynamic_cast<type>
<type> (expr)
(
6
E.x. 1. dynamicasts.cpp
2. dynamic_casts1.cpp
C++ dynamic_cast

z When dynamic_cast cannot cast a pointer because it


is not a complete object ofo the required class, it returns
a null pointer to indicate the failure.
z If dynamic_cast is used to convert to a reference type
and the conversion is not
no possible, an exception of
type bad_cast is thrown.
z dynamic_cast can also cast null pointers even
between pointers to unrelated classes.
z can also cast pointers of any type to void pointers
(void*).

7
C++ static_cast

z static_cast can perform conversions between


pointers to related classes,
classe not only from the derived
class to its base, but also from a base class to its
derived.
z ensures that at least the classes are compatible if the
proper object is converted
z but no safety check is performed during runtime to
check if the object being converted is in fact a full
object of the destination type.
z could lead to runtime errors
e.x. 1. static_casts4.cpp
8 2. static_casts3.cpp
C++ static_cast

z static_cast can also be used to perform any


other
th non-pointer conversion
conver that could also
be performed implicitly
z Or
O any conversioniion b
between
t classes
l with
ith
explicit constructors or operator functions

Syntax: static_cast<type>
<type> (expr)
(

9
C++ reinterpret_cast

z reinterpret_cast converts any pointer type to


any other i t ttype, even off unrelated
th pointer l t d
classes. Syntax: reinterpret_cast<type>
reinterpret_cast (expr)

class A {};
class B {};
A * a = new A;
B * b = reinterpret_cast<B*>(a);
reinterpret_cast

10
E.x.. 1. reinterprete_casts2.cpp
2. reinterprete_casts1.cpp
C++ const_cast

z const_cast manipulates the constness of an


object,
bj t either
ith to
t be
b set
ett or tto b
be removed.
d

Syntax: const_cast<type>
namespace (expr)

E.x. 1. const_cast1.cpp
2. const_casts2.cpp
11
cout<<“Thank You”<<endl;

cout<<“Have a Good Day”<<endl;

12

You might also like