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"

Problem with C++ casting

Hey, I would need a little help from someone, since 4.14 my code doesn't work right, I tried everything but this cast seems to resist everything, it would be cool if someone could help me: here is the broken code:

 ABaseGameMode * UStaticFunctionLibrary::GetGameModeCDO(UObject * WorldContextObject)
 {
     AGameStateBase * GameState = UGameplayStatics::GetGameState(WorldContextObject);
     if (GameState)
     {
         ABaseGameMode Cast = Cast<ABaseGameMode>(GameState->GetDefaultGameMode());
         return Cast;
     }
 
     return NULL;
 }

here are the error i keep getting : alt text

i hope someone can help me ! :D

Product Version: UE 4.14
Tags:
more ▼

asked Jan 05 '17 at 03:25 PM in C++ Programming

avatar image

BurstTheGame
13 3 5 8

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

1 answer: sort voted first

GetDefaultGameMode() returns const AGameModeBase*, and so Cast() will return const ABaseGameMode*. But you are trying to return it a non-const ABaseGameMode*.

You need to return const ABaseGameMode*.

Or cast away constness by a const_cast, but you probably don't want to do that.

Steve

more ▼

answered Jan 05 '17 at 03:33 PM

avatar image

Steve Robb STAFF
2.8k 58 31 102

avatar image BurstTheGame Jan 05 '17 at 03:47 PM

het thx for the answer, but I think the best option is to do a const_cast because I can't change the return type to a const, if I do that I get a ton of errors, so yeah. the problem I have is that I get a weird error with the const_cast :

alt text

avatar image Steve Robb STAFF Jan 05 '17 at 03:55 PM

Unlike UE4's Cast, C++'s const_cast requires you to put the asterisk to say that you're wanting a pointer. What you need is:

 const_cast<ABaseGameMode*>

The other errors you get will be because any code which calls GetGameModeCDO() will also need const-correcting:

 // error: this needs to be const ABaseGameMode*
 ABaseGameMode* GameMode = Library->GetGameModeCDO(Context);

... and so on, throughout the project:

https://isocpp.org/wiki/faq/const-correctness#retrofitting-const

This was added as an important safety feature, as it is not safe to modify CDOs. I would advise only doing the const_cast if you are absolutely sure that you're never going to be modifying the CDO via that returned pointer.

Steve

avatar image BurstTheGame Jan 05 '17 at 04:40 PM

hey thanks for the complete answer you gave me, ut i'll need you for a last thing :D, i tried doing it as you said, without the Cont_cast, but it is a staticfunctionlibrary so i decided to return to the first idea i had and to do a const_cast, i followed your step and now i have this problem ;

alt text

avatar image Steve Robb STAFF Jan 05 '17 at 04:43 PM

You need both const_cast and Cast:

 const_cast<ABaseGameMode*>(Cast<ABaseGameMode>(GameState->GetDefaultGameMode()));

Steve

avatar image BurstTheGame Jan 05 '17 at 04:45 PM

OHHH, ok i understand how it works now !! thanks for all the help !

(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