+ 1
can we take a data member by static_cast and assign to another ??
we have an "objectA" from classA that has a data member ptr and other members: void *ptr; we have ClassB that has a data member ctxptr. void * ctxptr; ClassB(classA & objectA) : ctxptr(static_cast <void*>(objectA) ) { } here use from member initialize list. I think it could as follow: ctxptr = objectA.ptr; but I can't understand another synatax.
3 ответов
+ 1
static_cast <type-id> ( expression )
Converts an expression to the type of type-id, based only on the types that are present in the expression.
The static_cast operator can be used for operations such as converting a pointer to a base class to a pointer to a derived class. Such conversions are not always safe.
static_cast conversions are not as safe as dynamic_cast conversions, because static_cast does no run-time type check, while dynamic_cast does.
A dynamic_cast to an ambiguous pointer will fail, while a static_cast returns as if nothing were wrong; this can be dangerous. Although dynamic_cast conversions are safer, dynamic_cast only works on pointers or references, and the run-time type check is an overhead.
https://code.sololearn.com/cuh4DJwwg6hm
more information:
https://docs.microsoft.com/en-us/cpp/cpp/static-cast-operator?view=vs-2019