Unreal Engine supports downcasting of UObject*
via Cast<T>
and via a custom implementation of dynamic_cast<T*>
.
When downcasting USTRUCT
objects, both methods don't work:
- The methods of
Cast<T>
do not support conversions toUScriptStructs
. - UE4 compiles without RTTI (e.g.
/GR-
is set for cl.exe) and UE4 uses thedynamic_cast<T*>
of the implementation for pointers toUSTRUCT
s. Therefore the compiler throws C4541 (see example below).
Is there a method in UE4.22 to safely downcast USTRUCT
s using the reflection system of UE4 (so when static_cast<T*>
etc is not an option)?
If not, then why UE4 doesn't support downcasting USTRUCT
s by its Cast functions? E.g. shouldn't they be referenced or are the reasons Blueprint related?
Example for (2), use within an UE4 project:
#pragma once
#include "CoreMinimal.h"
USTRUCT()
struct MyStructBase
{
virtual ~MyStructBase() = default;
};
USTRUCT()
struct MyStructDerived : public MyStructBase
{};
void TestFunc()
{
auto lvalue = MyStructBase{};
auto lvaluePtr = &lvalue;
auto o = dynamic_cast<MyStructDerived*>(lvaluePtr); // cl.exe throws C4541
}
Aucun commentaire:
Enregistrer un commentaire