Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>(Note that <code>class</code> isn't a valid variable name, since it's a keyword. I'll call it <code>c</code> instead).</p> <blockquote> <p>Is it safe to delete the pointer after dynamic_casting?</p> </blockquote> <p>Yes; but beware that both pointers are invalid after deleting the object that they point to. You can't use either pointer value afterwards.</p> <blockquote> <p>In general how dynamic_cast actually works?</p> </blockquote> <p>It converts a pointer or reference to a class type into a pointer or reference to a different class type, with a run-time check that the conversion is valid. In this case, the cast will succeed (giving a valid pointer) if <code>BaseClass</code> is the same as, or a base class of, the dynamic type of the object. It will fail (giving a null pointer) otherwise.</p> <p>If you were casting <code>*c</code> to a reference type, then failure would cause an exception (<code>std::bad_cast</code>), since there is no such thing as a null reference.</p> <blockquote> <p>does it work like a copy constructor?</p> </blockquote> <p>No. Copy constructors are for copying the object. This isn't copying it, just changing the type of a pointer that points to it. A copy would look like</p> <pre><code>BaseClass bc = *c; </code></pre> <p>Note that the type of <code>bc</code> is <code>BaseClass</code>, not the type of <code>c</code> (which is presumable a class derived from <code>BaseClass</code>); this is known as "slicing", since the derived part of the object is "sliced off" and not copied.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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