Please explain this C++ entry

Please explain this entry(what does it stand for) and why do you need a reference to InventoryItem in the loop ?

I`m assuming the loop will go through every item in InventoryReference assign a single item to InventoryItem temporary so it can be passed as a value to ItemSlot->ItemReference, finally InventoryPanel->AddChildToWrapBox add each item separately to each CreateWidget Box :smiley:

1 Like

So why do you need to use ref., and not just iterate without them.

From googleā€¦
improve code readability, facilitate efficient parameter passing, and avoid unnecessary object copying :wink:

I have never used ref. in loops, I just wrote like this :
for(FSlot Slot : Content){ā€¦}

But here arent you are creating a copyā€¦?

Yes indeed , you are copying the Reference to the local variable InventoryItem.

No, Iā€™m not creating it, but is it necessaryšŸ„¹?

Yes is necessary , for the loop to work. This kind of for loop was introduce in C++ 11 and is called ā€œfor-each loopā€.
which is used exclusively to loop through elements in an array.

My cycle works without themā€¦
:sob::sob::sob::sob:

in your example you doing the same think , you are copying each element of Content to the local variable Slot.

I see, Iā€™m really dumb, but why do I have to do it this way:

For(FSlot& Slot : Content){ā€¦}

I think itā€™s even written about it in the documentation

You are creating a copy. In (FSlot Slot : Content){ā€¦} FSlot Slot is a new variable in the scope of the ranged for loop. Any changes to Slot will not affect the elements in Content.

A copy is not neededā€¦ a reference is. The point behind for(const FSlot& Slot : Content) is to only work with a reference variable (8~ bytes on 64 bit) rather than duplicating everything, among other thingsā€¦

1 Like

if you dont use the ā€œ&ā€ you are creating a new variable (a Copy). when you use the ā€œ&ā€ you are refering the original variable without creating a copy.

1 Like

So it is necessary for optimization (to take less memory), right ? If not, please correct itšŸ˜‘

Yes. A reference is like an alias, if you modify the original variable it will also be reflected in it and vice versa since it reads from the same place. In the image of your first post a * was used, this is for a pointer.

const ensures it cannot be modified, only copied from. if you remove it and only use (FSlot& Slot : Content){ā€¦} any changed to Slot will also affect the index that was referenced from Content.

For example:

     std::vector<int> Content = {0, 1, 2, 3, 4, 5};

     for(auto Slot : Content)
     {
        ++Slot;
     } // Result: {0,1,2,3,4,5}

      for(auto& Slot : Content)
     {
        ++Slot;
     } // Result: {1,2,3,4,5,6}

      for(const auto& Slot : Content)
     {
        ++Slot; // will not compile.
     } 

You can experiment with code in websites like https://godbolt.org/. Here you also get to see what the compiler is doing (you can ignore it for now).

If you have access to ChatGPT 4 you can paste code and go line by line asking it to explain. Just remember that it can still make mistakes so always test and check with other sources.

Hope it helps. Feel free to ask anything.

THANK YOU SO MUCH @pezzott1 @TrueFranco . Thanks to you, I understand why I do all this, thank you :blush::blush::blush:.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.