HInoue
(HInoue)
December 22, 2015, 11:31pm
1
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?
1 Like
rantrod
(rantrod)
December 22, 2015, 11:47pm
2
if(FMyFirstChildStruct *FS = Cast(structList[index]) {…
HInoue
(HInoue)
December 22, 2015, 11:55pm
3
Ooh, I think that does it, thank you!
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
HInoue
(HInoue)
December 23, 2015, 12:25am
4
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!"));
}
rantrod
(rantrod)
December 23, 2015, 12:34am
5
You can make an enum and use the values as identifiers in each subobject that you can check against.
rantrod
(rantrod)
December 23, 2015, 12:37am
6
Here you go:
FTestChild1 TC1;
FTestChild1* testChildStruct = Cast<FTestChild1>(TC1);
if (testChildStruct) {
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Merry christmas!"));
}
HInoue
(HInoue)
December 23, 2015, 12:49am
7
Oh hey, that’s a much cleaner solution; thank you!!