Merging TArrays without changing them

Hi,

So I’m quite new to c++ and having some problems with pointers and references. I understand the basic concept of it, but especially when it comes to parameters and return values i have my problems.

But now to my question.

I have 2 TArrays of the same type. I want to write a function that returns the Arrays appended together, but I don’t want the elements of the new TArray to be copies and I don’t want to append something to the original TArrays.
The function is in the same class as the TArrays.

So those are my TArrays I want to use for the function:

TArray<UInventorySlot*> InputSlots;
TArray<UInventorySlot*> OutputSlots;

Thanks in advance!

I’d say something along the lines of something like this should do the trick.

TArray<UInventorySlot*> NewArray;

for(UInventorySlot* slot1 : InputSlots) {
      NewArray.Add(slot1);
}
    
for(UInventorySlot* slot2 : OutputSlots) {
      NewArray.Add(slot2);
}

Basically it says.

1 make a new Array

2 for everything in InputSlots add it to New Array.

3 for everything in OutputSlots add it to New Array.

If you want to make sure there are not repeat entries use AddUnique() instead.

Ok. Thanks for your fast reply.

And how consistent is this solution, meaning if I delete an element in the newarray or if the memory-location of one pointed-at-element changes are still all arrays consistent?

The main reason to use TArrays in the first place is to not worry to much about memory, UE4 cleans up after you for the most part. A good habit to get into regardless of how confident you are in what a pointer is pointing to is the check if it’s valid before using it.

This is a good example of how to do just that.

But if you are asking whether or not all three arrays will be constant with each other I.E remove and element from input and it get removed in new or remove something from new and it correctly removed from input and or output. This wont do that, you will have to actively maintain the list yourself at all time for that.

Ok. Thanks a lot for the help!