Yes, the second example is well defined, and the cast will succeed. During the constructor of B, the dynamic type of the object is B, so a cast to B* will succeed. In the first example, as you say, in the constructor of A the dynamic type is A, so a cast to B* will fail....
If you're going to down-cast like this, you generally need to check whether the conversion succeeded. In the specific case you've given, it won't succeed, so what gets assigned (the result of the dynamic_cast) will simply be a null pointer. Most of the time, you'd prefer to ...
static_castis thefirstcast you should attempt to use. It does things like implicit conversions between types (such as int to float, or pointer to void*), and it can also call explicit conversion functions (or implicit ones). In many cases, explicitly statingstatic_castisn't necessary, but ...
(*refPtr)) { - // not sure if this should succeed or return an error - return napi_ok; + auto* globalObject = toJS(env); + auto& vm = globalObject->vm(); + const JSC::Identifier& propertyName = WebCore::builtinNames(vm).napiWrappedContentsPrivateName(); + + if (!jsc_...
for the cast to succeed at run time. The cast is well-formed, regardless of whether the type of the most-derived object pointed to by expression (IOW, the dynamic type of *expression) inherits from the target type. However, it will return static_cast<A *>(nullptr) unless A is a publ...