I have read somewhere that if you dynamic_cast a void* you get the start of the address of the object ?
I have no clue about this. How is this possible.?
Can someone help me on this

Dani AI

Generated

The demo from is a good practical wake‑up: the pointer values printed there are not magic, they come from runtime type information and pointer adjustments that the implementation performs for polymorphic objects. That example shows the symptom; what follows explains why it happens and what to watch for.

Polymorphic objects carry metadata (vtable/RTTI) that records how each base subobject sits inside the complete, most‑derived object. When a runtime cast needs the complete object address, the implementation looks up that metadata and applies an offset from the current subobject pointer to the object's start. In multiple and virtual inheritance the subobject can be at a nonzero offset, so the computed address can differ from the original pointer. Common ABIs encode this offset alongside the vtable so dynamic casts can compute the adjustment efficiently (see the cppreference summary of dynamic_cast for language-level behavior).

One important gotcha that explains the changing addresses in the constructor/destructor traces: while an object is being constructed or destroyed its dynamic type is the type currently under construction/destruction. A runtime cast done from inside a base class ctor/dtor will therefore see a different dynamic type and produce a different adjusted address than the same cast made after full construction.

Practical notes: don’t rely on this for portable object layout assumptions; enable RTTI (casts of this kind require it), remember pointer casts to reference types throw on failure while pointer casts return nullptr, and accept the runtime cost of dynamic_cast. For debugging or diagnostics it is handy; for tight or low‑level code prefer explicit layout strategies or documented casts when the layout is known.

you cannot apply a dynamic_cast on a void* to get a non void pointer.
except in a trivial case, in dynamic_cast<T>(v), v should be a pointer to or an lvalue of a polymorphic type.
if T is a (cv qualified) void* , and v is a pointer to a polymorphic type, the result is a (cv qualified) void* pointer which points to the 'most derived object' pointed to by v.
here is an example:

#include <typeinfo>
#include <iostream>
using std::cout ;

struct A { virtual ~A() {} int a ; } ;
struct B : virtual A { int b ; } ; struct C : virtual A { int c ; } ;
struct D : virtual A, virtual B, virtual C
{
   D() { cout << "D::constructor - " ; foo() ; }
   ~D() { cout << "D::destructor - " ; foo() ; }
   void foo() { cout << dynamic_cast<void*>(this) << '\n' ; }
   int d ;
} ;
struct E : virtual A, virtual B, virtual C, virtual D { int e ; } ;

int main()
{
  E object ;
  E* pe = &object ;
  D* pd = pe ;
  C* pc = pe ;
  B* pb = pd ;
  A* pa = pc ;
  void* pv = &object ;

  cout << pv << '\n'
       << dynamic_cast<void*>(pa) << '\t' << pa << '\n'
       << dynamic_cast<void*>(pb) << '\t' << pb << '\n'
       << dynamic_cast<void*>(pc) << '\t' << pc << '\n'
       << dynamic_cast<void*>(pd) << '\t' << pd << '\n'
       << dynamic_cast<void*>(pe) << '\t' << pe << '\n' ;
  pe->foo() ; pd->foo() ;
}

>g++ -std=c++98 -Wall dyn_cast.cpp && ./a.out
D::constructor - 0xbfbfe990
0xbfbfe970
0xbfbfe970 0xbfbfe978
0xbfbfe970 0xbfbfe980
0xbfbfe970 0xbfbfe988
0xbfbfe970 0xbfbfe990
0xbfbfe970 0xbfbfe970
0xbfbfe970
0xbfbfe970
D::destructor - 0xbfbfe990

note the behaviour during construction/destruction of a base class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.