Array add and shallow Copy

TArray m_Arr[10];

void Fun(AActor _obj)
{
 m_Arr.add(_obj)
}

if i change value in m_Arr, original Value is changed too?

I am embressed that i have to add ‘*’

like this

TArray<AActor*> m_Arr[10];
    
void Fun(AActor* _obj)
{
 m_Arr.add(_obj)
}

What exactly is your question? Does your first example or second example not compile? Does it not do what you are expecting? What have you already tried and what were your results?

not compile problem.

if i compile first code, and call function.

i wonder that if i change value of m_Arr , original Value also is changed too or not.

if not, i have to compile second example

here is example code


void main(void)

{

float* pf = new float

*pf = 3;

function(*pf);

}

void function(float _f)

{

_f = 4;

}


after function pf value still 3, not changed
because of call by value.

I am confused by this C++ principle apply unreal Engine too

Hey 1205-

When passing information into a function call there are two ways to do so: pass-by-reference and pass-by-value. When you pass in a standard variable (bool, float, FString, etc.) it will use pass-by-value where it creates a copy of the variable and all changes to the value are done on the copy. When you pass in a pointer as a function parameter it uses pass-by-reference where instead of creating a copy of the variable it will pass in a reference to the variables location. So any changes to the variable that is pass-by-reference will affect the original variable outside of the function as well. With arrays, the first element of the array acts as a pointer to the memory location where the array is stored, so anytime an array is passed into a function it is considered to be pass-by-reference which is why changes inside the function affect the array outside of the function as well. Hopefully this clears up why you’re seeing the array value change when you call the function.

Cheers