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
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 |
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 |
Badboy is defined who has ALL the following properties: 1. Does not have a girlfriend and is not married. 2. He is not more than 23 years old. 3. The middle name should be "Singh" 4. The last name should have more than 4 characters. 5. The character 'a' should appear in the last name at least two times. 6. The name of one of his brothers should be "Ram" Write a method: boolean isBadBoy(boolean hasGirlFriend , boolean isMarried, int age , String middleName , String lastName , String[] brotherName); isHaveGirlFriend is true if the person has a girlfriend isMarried is true if the person is married age is the age of the person middleName is the middle name of the person lastName is the last name of the person brotherName is the array of the names of his brothers
What is data abstraction? How is it different from data encapsulation?
What is the best c++ compiler for windows 10?
Which sort does c++ use?
Explain what is class definition in c++ ?
How do I tokenize a string in c++?
Difference between Overloading and Overriding?
35 Answers Appnetix Techno, GameLoft, HP, IBM, NIIT, Rohde and Schwarz,
Explain the problem with overriding functions
What does the following code do: int c=0; cout< a) Undefined *Updated* b) 01 c) 00
What is command line arguments in C++? What are its uses? Where we have to use this?
Write a C++ Program to Generate Random Numbers between 0 and 100
How do you allocate and deallocate memory in C++?