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"

Bug on for each?

Hello, i have the following variable:

 TArray<FTest> Tests;

Where FTest is the following struct:

 USTRUCT()
  struct FTest
  {
      GENERATED_USTRUCT_BODY()
  
      float Var;
  
      UPROPERTY(EditDefaultsOnly, Category = "My category")
      float MaxVar;
  
      FTest() {}
  };

If i do the following:

 for(FTest Test : Tests)
       Test.Var = 25;

It will not change the values. Is the for each supposed to give copies of the actual values or is this a bug? If i use the regular for it works fine.

 for(uint8 i = 0 ; i < Tests.Num() ; i++)
       Tests[i].Var = 25;

Product Version: Not Selected
Tags:
more ▼

asked Oct 02 '14 at 01:44 PM in C++ Programming

avatar image

xlar8or
6k 162 129 305

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

1 answer: sort voted first

You're taking a copy in the loop and then changing that, you'll want to use a reference instead.

 for(FTest& Test : Tests)
     Test.Var = 25;
more ▼

answered Oct 02 '14 at 01:47 PM

avatar image

Jamie Dale STAFF
7.5k 177 67 305

avatar image xlar8or Oct 02 '14 at 01:52 PM

Thank you for clarifying that, i'm used to the java for each so that was probably what created the confusion :)

avatar image xlar8or Oct 02 '14 at 01:58 PM

So theorically you will never want to use without &, otherwise you will be working with a copy and that's not really helpful :P

avatar image Jamie Dale STAFF Oct 02 '14 at 02:13 PM

You may sometimes have a legitimate reason to want to perform a copy, but yeah, you'll typically see one of the following:

 for(FTest& Test : Tests)
     Test.Var = 25;
 
 for(const FTest& Test : Tests)
     Func(Test.Var);
(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