
- C++ Library - Home
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- The C++ STL Library
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- The C++ Advanced Library
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Memory::dynamic_pointer_cast
An operator known as a cast transforms data from one type to another. Dynamic casting in C++ is mostly employed for run-time safe downcasting. There needs to be one virtual function in the base class for dynamic cast to function. Only polymorphic base classes can utilize dynamic casts since they use this data to determine safe downcasting.
Syntax
Following is the syntax for C++ Memory::dynamic_pointer_cast −
shared_ptrdynamic_pointer_cast (const shared_ptr& sp) noexcept;
Parameters
sp − Its a shared pointer.
Example 1
Let's look into the following example, where we are going to perform dynamic_pointer_casting and getting the output.
#include#include using namespace std; class Base { virtual void dummy() {} }; class Derived: public Base { int a; }; int main (){ try { Base * pba = new Derived; Base * pbb = new Base; Derived * pd; pd = dynamic_cast (pba); if (pd==0) cout << "Welcome \n"; pd = dynamic_cast (pbb); if (pd==0) cout << "TutorialsPoint\n"; } catch (exception& e) { cout << "Exception: " << e.what(); } return 0; }
Output
Let us compile and run the above program, this will produce the following result −
TutorialsPoint
Example 2
Considering the following example, where the dynamic_pointer_cast go down/across class hierarchy.
#include#include class Base { public: int a; virtual void f() const { std::cout << "Welcome To\n"; } virtual ~Base() {} }; class Derived : public Base { public: void f() const override { std::cout << "TutoriaslPoint\n"; } ~Derived() {} }; int main(){ auto basePtr = std::make_shared (); std::cout << "Base pointer: "; basePtr->f(); auto derivedPtr = std::make_shared (); std::cout << "Derived pointer: "; derivedPtr->f(); basePtr = std::static_pointer_cast (derivedPtr); std::cout << "Base pointer to derived : "; basePtr->f(); auto downcastedPtr = std::dynamic_pointer_cast (basePtr); if (downcastedPtr) { std::cout << "Downcasted pointer : "; downcastedPtr->f(); } }
Output
On running the above code, it will display the output as shown below −
Base pointer: Welcome To Derived pointer: TutoriaslPoint Base pointer to derived : TutoriaslPoint Downcasted pointer : TutoriaslPoint
Example 3
In the following example we are going to use dynamic_pointer_cast and retriveing the output in both static and dynamic.
#include#include struct A { static const char* static_type; const char* dynamic_type; A(){ dynamic_type = static_type; } }; struct B: A { static const char* static_type; B(){ dynamic_type = static_type; } }; const char* A::static_type = "Hello"; const char* B::static_type = "Welcome"; int main (){ std::shared_ptr Result; std::shared_ptr bar; bar = std::make_shared(); Result = std::dynamic_pointer_cast(bar); std::cout << "Result in static type: " << Result->static_type << '\n'; std::cout << "Result in dynamic type: " << Result->dynamic_type << '\n'; return 0; }
Output
when the code gets executed, it will generate the output as shown below −
Result in static type: Hello Result in dynamic type: Welcome