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;
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 );
c++
FInventoryStack& GetStackFromIndex( int32 idx );
and
c++
const FInventoryStack& GetStackFromIndex( int32 idx ) const;
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.
5 Replies
Rex
Rex3w ago
This is C++ pass-by-reference, no UE or CSS tricks involved. In a nutshell, it works like a pointer but cannot be NULL. Main reasons I see pass-by-reference being used are: - to have out parameters (with names typically prefixed with out in the UE and CSS coding styles) - to return a reference to something so that it can be modified by the caller - to avoid creating a copy of something, but disallow any modifications by making it a const reference As for GetStackFromIndex: there's two overloads. The idea is to be able to get a non-const reference if you can modify the object the function is to be called on, but also be able to get a const reference if the object you're calling the function on is also const.
MrWolf
MrWolfOP3w ago
So if I make a variable
FInventoryStack MyStack; const int32 idx=5; bool Magic = GetStackFromIndex( idx, FInventoryStack& MyStack );
FInventoryStack MyStack; const int32 idx=5; bool Magic = GetStackFromIndex( idx, FInventoryStack& MyStack );
You're saying that I can use MyStack as if it was mInventoryStacks[5]? My understanding is that we are passing the reference of MyStack down, so MyStack is written to and read from. So this is essentially backwards from what I would expect, and the opposite of what youre telling me
MrWolf
MrWolfOP3w ago
Stack Overflow
C++: Argument Passing "passed by reference"
I understand as with any other variable, the type of a parameter determines the interaction between the parameter and its argument. My question is that what is the reasoning behind why you would
MrWolf
MrWolfOP3w ago
Based on the stack overflow, Id expect I'd allocate my very large thing, and then pass that very large thing by reference. Allowing the function to read and modify my very large thing without copying it. However in our case... I am trying to access the Objects very large thing. So if I pass my reference... I'd expect the Object to Copy it's very large thing, into my reference. Giving me a Copy of it, without modifying the Objects. Is my understanding incorrect?
Rex
Rex3w ago
Ah, didn't see the 3rd signature amidst all the text (formatting code with inline code blocks would've helped) Here I was referring to the two overloads returning a (const) FInventoryStack& I think neither of these is public. The only public overload is bool GetStackFromIndex(const int32 idx, FInventoryStack& out_stack) const and that's simply an out parameter (it copies the inventory stack into the by-ref parameter) Consider a function signature that looks like bool GetStackFromIndex(const int32 idx, FInventoryStack* out_stack). Which is valid C, FInventoryStack could be a typedef'd struct. And let's not care about whether this is a member function (it doesn't matter here), which is why I omitted the const. The implementation would be more or less like this:
bool GetStackFromIndex(const int32 idx, FInventoryStack* out_stack)
{
if (!out_stack) {
return false;
}

/* Do the same thing as the real function, knowing out_stack is valid */
*out_stack = (FInventoryStack)0; /* Placeholder */
}
bool GetStackFromIndex(const int32 idx, FInventoryStack* out_stack)
{
if (!out_stack) {
return false;
}

/* Do the same thing as the real function, knowing out_stack is valid */
*out_stack = (FInventoryStack)0; /* Placeholder */
}
Unlike pointers, references cannot be null, so it avoids having to null-check stuff all the time (and forgetting about it)
Want results from more Discord servers?
Add your server