# Please explain this C++ entry

**URL:** <https://forums.unrealengine.com/t/please-explain-this-c-entry/1686563>\
**Category:** C++\
**Tags:** syntax, unreal-engine, question, CPP\
**Created:** [February 13, 2024, 5:54pm UTC](https://forums.unrealengine.com/t/please-explain-this-c-entry/1686563 "2024-02-13T17:54:38Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![blAddddDe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@blAddddDe](https://forums.unrealengine.com/u/blAddddDe)\
**Post date:** [February 13, 2024, 5:54pm UTC](https://forums.unrealengine.com/t/please-explain-this-c-entry/1686563/1 "2024-02-13T17:54:38Z")

</div>

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

 ![IMG_20240213_205120](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/8/4/d846161628718f5c2cc9b19d96c3f8eea12350c9.jpeg)

---

<div class="post-metadata">

**Author:** ![TrueFranco](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/truefranco/32/1095055_2.png) [@TrueFranco](https://forums.unrealengine.com/u/TrueFranco)\
**Post date:** [February 13, 2024, 6:20pm UTC](https://forums.unrealengine.com/t/please-explain-this-c-entry/1686563/2 "2024-02-13T18:20:04Z")

</div>

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 😃

---

<div class="post-metadata">

**Author:** ![blAddddDe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@blAddddDe](https://forums.unrealengine.com/u/blAddddDe)\
**Post date:** [February 13, 2024, 6:47pm UTC](https://forums.unrealengine.com/t/please-explain-this-c-entry/1686563/3 "2024-02-13T18:47:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TrueFranco](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/truefranco/32/1095055_2.png) [@TrueFranco](https://forums.unrealengine.com/u/TrueFranco)\
**Post date:** [February 13, 2024, 6:54pm UTC](https://forums.unrealengine.com/t/please-explain-this-c-entry/1686563/4 "2024-02-13T18:54:05Z")

</div>

From google…  
**improve code readability, facilitate efficient parameter passing, and avoid unnecessary object copying** 😉

---

<div class="post-metadata">

**Author:** ![blAddddDe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@blAddddDe](https://forums.unrealengine.com/u/blAddddDe)\
**Post date:** [February 13, 2024, 6:57pm UTC](https://forums.unrealengine.com/t/please-explain-this-c-entry/1686563/5 "2024-02-13T18:57:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pezzott1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pezzott1/32/1215068_2.png) [@pezzott1](https://forums.unrealengine.com/u/pezzott1)\
**Post date:** [February 13, 2024, 6:59pm UTC](https://forums.unrealengine.com/t/please-explain-this-c-entry/1686563/6 "2024-02-13T18:59:03Z")

</div>

> [@blAddddDe](#):
>
> for(FSlot Slot : Content){…}

But here arent you are creating a copy…?

---

<div class="post-metadata">

**Author:** ![TrueFranco](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/truefranco/32/1095055_2.png) [@TrueFranco](https://forums.unrealengine.com/u/TrueFranco)\
**Post date:** [February 13, 2024, 7:00pm UTC](https://forums.unrealengine.com/t/please-explain-this-c-entry/1686563/7 "2024-02-13T19:00:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![blAddddDe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@blAddddDe](https://forums.unrealengine.com/u/blAddddDe)\
**Post date:** [February 13, 2024, 7:02pm UTC](https://forums.unrealengine.com/t/please-explain-this-c-entry/1686563/8 "2024-02-13T19:02:58Z")

</div>

No, I’m not creating it, but is it necessary🥹?

---

<div class="post-metadata">

**Author:** ![TrueFranco](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/truefranco/32/1095055_2.png) [@TrueFranco](https://forums.unrealengine.com/u/TrueFranco)\
**Post date:** [February 13, 2024, 7:08pm UTC](https://forums.unrealengine.com/t/please-explain-this-c-entry/1686563/9 "2024-02-13T19:08:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![blAddddDe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@blAddddDe](https://forums.unrealengine.com/u/blAddddDe)\
**Post date:** [February 13, 2024, 7:11pm UTC](https://forums.unrealengine.com/t/please-explain-this-c-entry/1686563/10 "2024-02-13T19:11:55Z")

</div>

My cycle works without them…  
😭😭😭😭

---

<div class="post-metadata">

**Author:** ![TrueFranco](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/truefranco/32/1095055_2.png) [@TrueFranco](https://forums.unrealengine.com/u/TrueFranco)\
**Post date:** [February 13, 2024, 7:15pm UTC](https://forums.unrealengine.com/t/please-explain-this-c-entry/1686563/11 "2024-02-13T19:15:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![blAddddDe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@blAddddDe](https://forums.unrealengine.com/u/blAddddDe)\
**Post date:** [February 13, 2024, 7:18pm UTC](https://forums.unrealengine.com/t/please-explain-this-c-entry/1686563/12 "2024-02-13T19:18:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pezzott1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pezzott1/32/1215068_2.png) [@pezzott1](https://forums.unrealengine.com/u/pezzott1)\
**Post date:** [February 13, 2024, 7:19pm UTC](https://forums.unrealengine.com/t/please-explain-this-c-entry/1686563/13 "2024-02-13T19:19:45Z")

</div>

> [@blAddddDe](#):
>
> No, I’m not creating it, but is it necessary🥹?

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…

---

<div class="post-metadata">

**Author:** ![TrueFranco](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/truefranco/32/1095055_2.png) [@TrueFranco](https://forums.unrealengine.com/u/TrueFranco)\
**Post date:** [February 13, 2024, 7:23pm UTC](https://forums.unrealengine.com/t/please-explain-this-c-entry/1686563/14 "2024-02-13T19:23:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![blAddddDe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@blAddddDe](https://forums.unrealengine.com/u/blAddddDe)\
**Post date:** [February 13, 2024, 7:33pm UTC](https://forums.unrealengine.com/t/please-explain-this-c-entry/1686563/15 "2024-02-13T19:33:56Z")

</div>

So it is necessary for optimization (to take less memory), right ? If not, please correct it😑

---

<div class="post-metadata">

**Author:** ![pezzott1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pezzott1/32/1215068_2.png) [@pezzott1](https://forums.unrealengine.com/u/pezzott1)\
**Post date:** [February 13, 2024, 7:50pm UTC](https://forums.unrealengine.com/t/please-explain-this-c-entry/1686563/16 "2024-02-13T19:50:16Z")

</div>

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:

```auto
     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/](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.

---

<div class="post-metadata">

**Author:** ![blAddddDe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@blAddddDe](https://forums.unrealengine.com/u/blAddddDe)\
**Post date:** [February 13, 2024, 8:03pm UTC](https://forums.unrealengine.com/t/please-explain-this-c-entry/1686563/17 "2024-02-13T20:03:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [March 14, 2024, 8:03pm UTC](https://forums.unrealengine.com/t/please-explain-this-c-entry/1686563/18 "2024-03-14T20:03:54Z")

</div>

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