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"

Casting Pointers in C++

Assuming we have 2 simple classes like this:

 UCLASS()
 class ELEMENTAL_API UParentClass : public UObject
 {
     GENERATED_BODY()
 public:
     UFUNCTION(BlueprintCallable, Category = "Info")
     void Refresh();
 };
 
 
 UCLASS()
 class ELEMENTAL_API UChildClass : public UParentClass
 {
     GENERATED_BODY()
 public:
     UFUNCTION(BlueprintCallable, Category = "Info")
     void Refresh();
 };

then, if I want to cast a pointer to UParentClass to a pointer to UChildClass, like below:

 void RefreshData(UParentClass* data)
 {
      const UChildClass* ptr = Cast<UChildClass>(data); // successful cast
      ptr->Refresh(); // alwasy compile error here
 };

I always end up with a constant pointer to UChildClass. The problem is, I can't call a function from that casted pointer. It won't compile and shows this error:

 cannot convert 'this' pointer from 'const UChildClass' to 'UChildClass &'

I believe it's because it's not allowed to do this on a constant pointer. Is it really like that?

Another question is, is there a way to cast a pointer to a non-constant pointer in UE?

Please note that I want to do safe-casting. So direct casting like this (ordinary C++ style):

       const UChildClass* ptr = (UChildClass*)data;

is not preferable, because I don't know if it will cause run-time error, if the casting failed, or if it will be casted to a pointer with invalid memory address. In other words, it might be more difficult to handle problems with ordinary C++ style casting.

But, if that's the only way, then I'll just have to accept it.

Product Version: UE 4.12
Tags:
more ▼

asked Aug 24 '16 at 12:34 PM in C++ Programming

avatar image

Pelangi
266 22 34 42

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

1 answer: sort voted first

Sorry, my mistake. I accidentally declared the function parameter to be const. After I removed the const keyword, it compiled successfully.

more ▼

answered Aug 24 '16 at 12:42 PM

avatar image

Pelangi
266 22 34 42

(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