Search code examples
c++c++11castingvoid-pointersdynamic-cast

When is dynamic_cast<void*> useful?


5.2.7/7 says something along the lines of:

If T is "pointer to cv void", the result is a pointer to the most derived class pointed to by x.

What is a good application of this syntax? When should dynamic_cast<void*> be used?


Solution

  • One common reason is to figure out whether two interfaces IA* and IB* are in fact pointers to the same underlying object. If you need that, use the cast.

    IIRC, it's even possible in case of Multiple Inheritance with a repeated non-virtual base to have two IA* pointers which compare unequal, yet point to the same object - because they point to two different IA subobjects.