Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 863
Mason Wheeler Avatar asked Aug 21 '14 01:08

Mason Wheeler


People also ask

What does casting in C do?

Type Casting is basically a process in C in which we change a variable belonging to one data type to another one. In type casting, the compiler automatically changes one data type to another one depending on what we want the program to do.

What is code casting?

Codecasting is the process of recording your coding session, and casting it out to any number of participants, generally in real-time. JS Bin supports codecasting out of the box, for free, to both registered and anonymous users.

Does C do implicit casting?

Implicit type conversion in C happens automatically when a value is copied to its compatible data type. During conversion, strict rules for type conversion are applied. If the operands are of two different data types, then an operand having lower data type is automatically converted into a higher data type.

What is type casting in c3?

Type casting is when you assign a value of one data type to another type.


1 Answers

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.

like image 192
aschepler Avatar answered Oct 13 '22 04:10

aschepler