Force return-by-reference function to make a copy?

Started by
12 comments, last by SiCrane 11 years, 4 months ago
Hey GameDev,

I'm in sort of a weird spot with my linked list class as part of a larger assignment. For my node class, my GetData function returns by reference. The template functions look like this:

// Node's GetData functions.
const T& GetData const { return m_data; }
T& GetData { return m_data; }

It seemed like a good idea for the Delete function in my linked list to return the node's data back to the user after deletion. However I'm not sure if it's even possible to return the data after, since my node's getter functions are returning by reference. Can you force a return-by-value from a return-by-reference function? Is there a better solution to this? Thanks.
Advertisement
It seemed like a good idea for the Delete function in my linked list to return the node's data back to the user after deletion. However I'm not sure if it's even possible to return the data after, since my node's getter functions are returning by reference. Can you force a return-by-value from a return-by-reference function? Is there a better solution to this? Thanks.
That only sounds like a good idea if the lists job is just to maintain ordering, and not to own the memory management of the items. I.e. it would not actually do any deletion, just item removal. Otherwise the Delete function has no business giving anything back to the caller.

You can't change the behaviour of a reference.

We need more information about how the class works and what its purpose is to answer more thoroughly. I.e. is just the data part itself dynamically allocated? Does that happen outside or inside of the class? Just show more code rather than trying to answer these questions.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

[quote name='Robot Ninja' timestamp='1355607428' post='5011043']It seemed like a good idea for the Delete function in my linked list to return the node's data back to the user after deletion. However I'm not sure if it's even possible to return the data after, since my node's getter functions are returning by reference. Can you force a return-by-value from a return-by-reference function? Is there a better solution to this? Thanks.
That only sounds like a good idea if the lists job is just to maintain ordering, and not to own the memory management of the items. I.e. it would not actually do any deletion, just item removal. Otherwise the Delete function has no business giving anything back to the caller.

You can't change the behaviour of a reference.

We need more information about how the class works and what its purpose is to answer more thoroughly. I.e. is just the data part itself dynamically allocated? Does that happen outside or inside of the class? Just show more code rather than trying to answer these questions.
[/quote]
The linked list allocates the nodes dynamically, and is in fact an ordered list. The node itself contains the data, and only one next pointer, so obviously I'm dealing with a singly linked list. The list dynamically allocates the nodes inside its Insert function:

template <typename T>
void SortedList<T>::Insert( const T &amp;item )
{
// Make new node the front if it has the 'lowest' value, or list is empty
if (m_pFront == nullptr || item < m_pFront->Data())
m_pFront = new ListNode(item, m_pFront);
else
{
ListNode *pLinkingNode = m_pFront; // newNode will be pointed to by this.
// Keep iterating until we find the 'in-order' pos, until we hit the end.
while (pLinkingNode->Next() != nullptr &amp;&amp;
pLinkingNode->Next()->Data() < item )
{
pLinkingNode = pLinkingNode->Next();
}

// Insert the node in its appropriate position
ListNode *pNewNode = new ListNode(item, pLinkingNode->Next());
pLinkingNode->SetNext(pNewNode);
}

m_count++;
}

Though it would be easier to use a doubly linked list, I created the class for a previous assignment and didn't want to revamp the list implementation when I started this assignment. I'm considering changing it now...

As a whole the list is supposed to serve my chained hash table, and then my hash table serves as an allocater for my custom language interpreter. I'm still figuring out how to tokenize the language syntax, but that topic should probably be in a different thread.
If you want to return a copy of the data deleted after deleting a node, overload the DeleteNode function (or use a different name) with one that takes a reference to a node type, and copy the data into the provided reference, then do the deletion.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

If you want to return a copy of the data deleted after deleting a node, overload the DeleteNode function (or use a different name) with one that takes a reference to a node type, and copy the data into the provided reference, then do the deletion.

I could do that, but the Delete function in my list class gets called by the Delete function in my hash table class (also designed to return the data). The data would still need to be copied from that level, unless I also made another similar Delete function in my hash table as well. It seems a little wacky to me to do that though...
Well I wouldn't have my delete function do anything except delete things. If they want a copy of the data, make the client code call a function to make a copy then delete the node. Otherwise you pay the price for a copy every time you delete anything.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Well I wouldn't have my delete function do anything except delete things. If they want a copy of the data, make the client code call a function to make a copy then delete the node. Otherwise you pay the price for a copy every time you delete anything.


I might just have to do that then. I guess the only thing that should be returning data after removing it is a stack? laugh.png
Nope, pop() in a std::stack just removes the top of the stack, it makes you call stack::top beforehand if you want the value, for similar reasons ;)

I quite often return the old value from functions which replace a value with another though (e.g. set a flag bit returns the old value of the flag), but not with generic types (although probably ok with return value optimisation and/or move constructor thingios).
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Nope, pop() in a std::stack just removes the top of the stack, it makes you call stack::top beforehand if you want the value, for similar reasons ;)


Yikes, I've obviously been doing it wrong then. :(


I quite often return the old value from functions which replace a value with another though (e.g. set a flag bit returns the old value of the flag), but not with generic types (although probably ok with return value optimisation and/or move constructor thingios).

What does return value optimization consist of? All I know about the move constructor is that it's a C++11 thing. I'll have to visit that after I understand all the pre-C++11 essentials. tongue.png
Basically, if you return by value but you don't store the value, the compiler can optimise it out.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement