C++ Pass by reference

I am very familiar with C and Firmware development. I've dabbled in C++ but for higher level programming at a professional level I use Python and GoLang the most.

Could someone take a second to explain how pass by reference works in this specific case?
c++
bool GetStackFromIndex( const int32 idx, FInventoryStack& out_stack ) const;

In theory... this should imply that if I make a FInventoryStack and pass it into this function, that my FInventoryStack will be operated on.

That is to say, instead of using the instance of UFGInventoryComponent's memory, I have created a new block of memory that will be written to.
So either the stack gets copied over to my memory, or only modifications made to out_stack during the function call are written to my memory.

I would think that would be exactly what we do not want.

I would expect to have a function that returns me a pointer to the stack, so that we can call operations on that stack.

Am I just not understanding how C++ works? Or maybe this is a weird case because I see an example later in the header file that I think might be what I'm after, but they are protected
c++
FInventoryStack& GetStackFromIndex( int32 idx );
and
c++
const FInventoryStack& GetStackFromIndex( int32 idx ) const;


My basic understanding is that when a reference is returned, unlike return by value which calls the copy-constructor, instead we pass back a reference to the memory.

Depending on how CSS returned that variable there might be some issues tho.

I really need to not copy memory all over the place. Hoping someone has some information.
Was this page helpful?