How do I determine which subclass an object is?

Let’s say I have some base struct, and two child subclasses:

USTRUCT()
struct FMyBasicStruct{
    GENERATED_BODY()
};

USTRUCT()
struct FMyFirstChildStruct : public FMyBasicStruct{
    GENERATED_BODY()
};

USTRUCT()
struct FMySecondChildStruct : public FMyBasicStruct{
    GENERATED_BODY()
};

I then create an array of the base class, which means that it can hold any child that inherits from the base class:

TArray<FMyBasicStruct> structList;

If I were to iterate through every member of the array, is there any way to identify which exact subclass of FMyBasicStruct each member is?

if(FMyFirstChildStruct *FS = Cast(structList[index]) {…

Ooh, I think that does it, thank you! :slight_smile:

There’s no way to get that to work in a switch block, is there? The conditionals work if nothing else does, but this is going to get really hard to indent when I get to 20-30 children :slight_smile:

Hm, quick follow-up, am I doing this correctly? This version seems more straightforward but it doesn’t compile, it claims “None of the overloads could convert all argument types”:

FTestChild1 TC1;
	FTestChild1* testChildStruct = Cast<FTestChild1*>(TC1);
	if (testH) {
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Merry christmas!"));
	}

You can make an enum and use the values as identifiers in each subobject that you can check against.

Here you go:

FTestChild1 TC1;
FTestChild1* testChildStruct = Cast<FTestChild1>(TC1);
if (testChildStruct) {
    GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Merry christmas!"));
}

Oh hey, that’s a much cleaner solution; thank you!! :slight_smile: