Why does casting a struct cause an error?

I have a base struct FMyStructBase, and a bunch of child structs FMyStructChild : public FMyStructBase. I’m iterating through a TArray, identifying which subchild each member is, and then handling each child differently.

Where I’m running into trouble is accessing data unique to the child class. As an example, say I have the following practice version:

struct FMyChildStruct : public FMyBaseStruct {

		float uniqueFloat;

struct FMyBaseStruct {


In order to access the float, I take a member I know for a fact to belong to the correct child class, and try to cast it:

FMyBaseStruct baseVersion;
FMyChildStruct childVersion;

baseVersion = childVersion;

FMyChildStruct tempChild = Cast<FMyChildStruct>(childVersion); //Cast the object to the desired subclass
if (childVersion) { //If the cast succeeded...
	return childVersion.uniqueFloat; //Access its unique members and do whatever

However, it really doesn’t like my cast; this returns an error at compile-time, “None of the 2 overloads could convert all the argument types”. Is it not possible to cast structs at all?

Just gonna go out on a limb and say, because it isn’t a pointer? In this case i’d guess it would be being asked to make a copy.

How about:

FMyChildStruct* tempChild = Cast<FMyChildStruct>(&childVersion);

The whole idea of casting is to reinterpret the underlying bytes (and thus operate on memory addresses) through pointers as different data-types. The idea of casting doesn’t make a lot of sense otherwise.

Oh hey, great call; I had no idea that casting operated that way, that explains a lot.

Using your version verbatim produces an error, “conversion requires reinterpret_cast, C-style cast or function-style cast”, but changing it to a C-style cast compiles cleanly, thank you very much! :slight_smile:

Hey, Can u share the code of c-style cast.

And if I want to dynamically cast like if i have 2 child structs and the i want to dynamic_cast(…) How to do this?