How many types of casting are there in C++?
When is a dynamic cast,static_cast,reinterpret cast used?

Answers were Sorted based on User's Feedback



How many types of casting are there in C++? When is a dynamic cast,static_cast,reinterpret cast use..

Answer / berzerk

Casting Operators
There are several casting operators specific to the C++ language. These operators are intended to remove some of the ambiguity and danger inherent in old style C language casts. These operators are:

dynamic_cast Used for conversion of polymorphic types.

static_cast Used for conversion of nonpolymorphic types.

const_cast Used to remove the const, volatile, and __unaligned attributes.

reinterpret_cast Used for simple reinterpretation of bits.

Is This Answer Correct ?    4 Yes 0 No

How many types of casting are there in C++? When is a dynamic cast,static_cast,reinterpret cast use..

Answer / shakti singh khinchi

There are 4 types of castings in C++ which is known as RTTI
(Run Time Type Identification).
Dynamic cast: When dynamic_cast cannot cast a pointer
because it is not a complete object of the required class
-as in the second conversion in the previous example- it
returns a null pointer to indicate the failure. If
dynamic_cast is used to convert to a reference type and the
conversion is not possible, an exception of type bad_cast is
thrown instead.

dynamic_cast can also cast null pointers even between
pointers to unrelated classes, and can also cast pointers of
any type to void pointers (void*).

static cast:static_cast can perform conversions between
pointers to related classes, not only from the derived class
to its base, but also from a base class to its derived. This
ensures that at least the classes are compatible if the
proper object is converted, 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.

const cast:This type of casting manipulates the constness of
an object, either to be set or to be removed

reinterpret cast;reinterpret_cast converts any pointer type
to any other pointer type, even of unrelated classes. The
operation result is a simple binary copy of the value from
one pointer to the other. All pointer conversions are
allowed: neither the content pointed nor the pointer type
itself is checked.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C++ General Interview Questions

Explain deep copy?

0 Answers  


Can a program run without main function?

0 Answers  


What is nested class in c++?

0 Answers  


What is late binding c++?

0 Answers  


an operation between an integer and real always yeilds a) integer result b) real result c) float result

0 Answers  


Difference between a copy constructor and an assignment operator.

0 Answers  


Write a program which is required to process the time of a clock in hours and minutes, entered from the keyboard. With this program, there are two requirements for any data entered by a user: 1. The data must be of the correct type (in this case, two ints). 2. The data must be in the correct range: this means that, for the minutes, negative numbers and any number above 59 must be rejected; for the hours, negative numbers and any number above 23 must be rejected. Output error message for invalid data input. Output the time one and a half hour after the time input. i.e. Hour: 22 Min: 32 One and a half hour after 22:32 is 00:02

0 Answers  


What are arrays c++?

0 Answers  


Is there a sort function in c++?

0 Answers  


What can I use instead of namespace std?

0 Answers  


Describe private, protected and public?

0 Answers  


What is the difference between public, private, protected inheritance?

12 Answers   Wipro,


Categories