x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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:

 USTRUCT(BlueprintType)
 struct FMyChildStruct : public FMyBaseStruct {
     GENERATED_BODY()
 
         UPROPERTY()
         float uniqueFloat;
 };
 
 USTRUCT(BlueprintType)
 struct FMyBaseStruct {
     GENERATED_BODY()
 
 };


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?

Product Version: UE 4.10
Tags:
more ▼

asked Dec 23 '15 at 04:04 AM in C++ Programming

avatar image

HInoue
251 96 66 83

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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.

more ▼

answered Dec 23 '15 at 06:15 AM

avatar image

DeckleSpec
1 1 1 1

avatar image HInoue Dec 23 '15 at 07:03 AM

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! :)

avatar image Ali Sheharyar Feb 03 '16 at 04:39 AM

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?

Thanks

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question