Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>**Are there any cases where we do down casting of objects** </code></pre> <p>The purpose of dynamic_cast is to perform casts on polymorphic types. For example, given two polymorphic classes Band D, with D derived from B, a dynamic_cast can always cast a D* pointer into a B* pointer. This is because a base pointer can always point to a derived object. But a dynamic_cast can cast a B* pointer into a D* pointer only if the object being pointed to actually is a D object.</p> <pre><code>**`Is there any better way to achieve the same`** </code></pre> <p>Perhaps the most important of the new casting operators is dynamic_cast. The dynamic_cast performs a run-time cast that verifies the validity of a cast.</p> <p>1) Your class is not polymorphic.A class that declares or inherits a virtual function is called a polymorphic class</p> <p>2) Syntax of dynamic_cast is <strong>dynamic__cast (expr)</strong></p> <p><strong>1st Edit :</strong></p> <p>Try like this , it will work</p> <pre><code>class A { public: A(); virtual ~A();// Notice here i have put virtual }; class AImpl : public A { public: AImpl(A *obj); ~AImpl(); }; class Helper { public: Helper(){} static bool doWork(A *obj) { AImpl *objImpl = dynamic_cast&lt;AImpl*&gt; (obj); return true; } }; </code></pre> <p>Study this example : </p> <pre><code>class Base { public: virtual void f() { cout &lt;&lt; "Inside Base\n"; } // ... }; class Derived: public Base { public: void f() { cout &lt;&lt; "Inside Derived\n"; } }; int main() { Base *bp, b_ob; Derived *dp, d_ob; dp = dynamic_cast&lt;Derived *&gt; (&amp;d_ob); if(dp) { cout &lt;&lt; "Cast from Derived * to Derived * OK.\n"; dp-&gt;f(); } else cout &lt;&lt; "Error\n"; cout &lt;&lt; endl; bp = dynamic_cast&lt;Base *&gt; (&amp;d_ob); if(bp) { cout &lt;&lt; "Cast from Derived * to Base * OK.\n"; bp-&gt;f(); } else cout &lt;&lt; "Error\n"; cout &lt;&lt; endl; bp = dynamic_cast&lt;Base *&gt; (&amp;b_ob); if(bp) { cout &lt;&lt; "Cast from Base * to Base * OK.\n"; bp-&gt;f(); } else cout &lt;&lt; "Error\n"; cout &lt;&lt; endl; dp = dynamic_cast&lt;Derived *&gt; (&amp;b_ob); if(dp) cout &lt;&lt; "Error\n"; else cout &lt;&lt; "Cast from Base * to Derived * not OK.\n"; cout &lt;&lt; endl; bp = &amp;d_ob; // bp points to Derived object dp = dynamic_cast&lt;Derived *&gt; (bp); if(dp) { cout &lt;&lt; "Casting bp to a Derived * OK\n" &lt;&lt; "because bp is really pointing\n" &lt;&lt; "to a Derived object.\n"; dp-&gt;f(); } else cout &lt;&lt; "Error\n"; cout &lt;&lt; endl; bp = &amp;b_ob; // bp points to Base object dp = dynamic_cast&lt;Derived *&gt; (bp); if(dp) cout &lt;&lt; "Error"; else { cout &lt;&lt; "Now casting bp to a Derived *\n" &lt;&lt; "is not OK because bp is really \n" &lt;&lt; "pointing to a Base object.\n"; } cout &lt;&lt; endl; dp = &amp;d_ob; // dp points to Derived object bp = dynamic_cast&lt;Base *&gt; (dp); if(bp) { cout &lt;&lt; "Casting dp to a Base * is OK.\n"; bp-&gt;f(); } else cout &lt;&lt; "Error\n"; return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload