Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Casting pointers to <code>void*</code> has its importance since way back in C days. Most suitable place is inside the memory manager of Operating System. It has to store all the pointer and the object of what you create. By storing it in void* they generalize it to store any object on to the memory manager data structure which could be <code>heap/B+Tree</code> or simple <code>arraylist</code>.</p> <p>For simplicity take example of creating a <code>list</code> of generic items(List contains items of completely different classes). That would be possible only using <code>void*</code>.</p> <p>standard says that dynamic_cast should return null for illegal type casting and standard also guarantees that any pointer should be able to type cast it to void* and back from it with only exception of function pointers.</p> <p>Normal application level practical usage is very less for <code>void*</code> typecasting but it is used extensively in low level/embedded systems.</p> <p>Normally you would want to use reinterpret_cast for low level stuff, like in 8086 it is used to offset pointer of same base to get the address but not restricted to this.</p> <p><strong>Edit:</strong> Standard says that you can convert any pointer to <code>void*</code> even with <code>dynamic_cast&lt;&gt;</code> but it no where states that you can not convert the <code>void*</code> back to the object.</p> <p>For most usage, its a one way street but there are some unavoidable usage.</p> <p>It just says that <code>dynamic_cast&lt;&gt;</code> needs type information for converting it back to the requested type.</p> <p>There are many API's that require you to pass <code>void*</code> to some object eg. java/Jni Code passes the object as <code>void*</code>.<br> Without type info you cannot do the casting.<strong>If you are confident enough that type requested is correct</strong> you can ask compiler to do the <code>dynmaic_cast&lt;&gt;</code> with a trick.</p> <p><strong>Look at this code:</strong></p> <pre><code>class Base_Class {public : virtual void dummy() { cout&lt;&lt;"Base\n";} }; class Derived_Class: public Base_Class { int a; public: void dummy() { cout&lt;&lt;"Derived\n";} }; class MostDerivedObject : public Derived_Class {int b; public: void dummy() { cout&lt;&lt;"Most\n";} }; class AnotherMostDerivedObject : public Derived_Class {int c; public: void dummy() { cout&lt;&lt;"AnotherMost\n";} }; int main () { try { Base_Class * ptr_a = new Derived_Class; Base_Class * ptr_b = new MostDerivedObject; Derived_Class * ptr_c,*ptr_d; ptr_c = dynamic_cast&lt; Derived_Class *&gt;(ptr_a); ptr_d = dynamic_cast&lt; Derived_Class *&gt;(ptr_b); void* testDerived = dynamic_cast&lt;void*&gt;(ptr_c); void* testMost = dynamic_cast&lt;void*&gt;(ptr_d); Base_Class* tptrDerived = dynamic_cast&lt;Derived_Class*&gt;(static_cast&lt;Base_Class*&gt;(testDerived)); tptrDerived-&gt;dummy(); Base_Class* tptrMost = dynamic_cast&lt;Derived_Class*&gt;(static_cast&lt;Base_Class*&gt;(testMost)); tptrMost-&gt;dummy(); //tptrMost = dynamic_cast&lt;AnotherMostDerivedObject*&gt;(static_cast&lt;Base_Class*&gt;(testMost)); //tptrMost-&gt;dummy(); //fails } catch (exception&amp; my_ex) {cout &lt;&lt; "Exception: " &lt;&lt; my_ex.what();} system("pause"); return 0; } </code></pre> <p>Please correct me if this is not correct in any way.</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. 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.
 

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