Search code examples
c++castingsemantics

What is this C++ casting code doing?


Found here: https://github.com/tpaviot/oce/blob/master/src/BRepAdaptor/BRepAdaptor_Curve.cxx

The line I'm wondering about is:

((GeomAdaptor_Curve*) (void*) &myCurve)->Load(C,First,Last);

myCurve is already defined as a GeomAdaptor_Curve. So it looks like it's casting a pointer to myCurve as a void*, and then casting that as a GeomAdaptor_Curve*, and then dereferencing it and calling Load on it. What possible reason could there be for doing that, rather than simply calling myCurve.Load?


Solution

  • Note that statement appears in a const member function. So the type of &myCurve is actually GeomAdaptor_Curve const*. This appears to be an ugly and confusing way to say

    const_cast<GeomAdaptor_Curve&>(myCurve).Load(C,First,Last);
    

    and may have been made more complicated in order to "avoid" compiler warnings you would get from attempting to use a C-style cast to circumvent const.