Posts

Showing posts with the label TYPECASTING

TYPECASTING IN C++ (DYNAMIC_CAST, STATIC_CAST, REINTERPRET_CAST, CONST_CAST)

TYPE CASTING: Explicit type casting between classes have four specific casting operators:   1) dynamic_cast <new_type> (expression)           2) static_cast <new_type> (expression)           3) reinterpret_cast <new_type> (expression)           4) const_cast <new_type> (expression) 1) Dynamic Cast: a) dynamic_cast can be used only with pointers and references to objects. b) Its purpose is to ensure that the result of the type conversion is a valid complete object of the requested class. c) When dynamic_cast cannot cast a pointer because it is not a complete object of the required class, it returns a null pointer to indicate the failure. class CBase {}; class CDerived : public CBase {};   CBase b; CBase* pb; CDerived d;  CDerived* pd; pb = dynamic_cast<CBase*>(&d);    ...