Sign in to follow this  
deus.ex.nova

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

Recommended Posts

deus.ex.nova    569
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:
[CODE]
// Node's GetData functions.
const T& GetData const { return m_data; }
T& GetData { return m_data; }
[/CODE]
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. Edited by Robot Ninja

Share this post


Link to post
Share on other sites
iMalc    2466
[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.
[/quote]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.

Share this post


Link to post
Share on other sites
deus.ex.nova    569
[quote name='iMalc' timestamp='1355608850' post='5011046']
[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.
[/quote]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:
[CODE]
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++;
}
[/CODE]
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. Edited by Robot Ninja

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
deus.ex.nova    569
[quote name='Paradigm Shifter' timestamp='1355611300' post='5011062']
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.
[/quote]
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... Edited by Robot Ninja

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
deus.ex.nova    569
[quote name='Paradigm Shifter' timestamp='1355611917' post='5011069']
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.
[/quote]

I might just have to do that then. I guess the only thing that should be returning data after removing it is a stack? [img]http://public.gamedev.net//public/style_emoticons/default/laugh.png[/img]

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
deus.ex.nova    569
[quote name='Paradigm Shifter' timestamp='1355612331' post='5011073']
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 ;)
[/quote]

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

[quote name='Paradigm Shifter' timestamp='1355612331' post='5011073']
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).
[/quote]
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. [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img]

Share this post


Link to post
Share on other sites
SiCrane    11839
If you really wanted to do this you could use:
[code]
T SortedList<T>::Delete(void) {
T temp(m_pFront->GetData());
// do the deletion logic
return temp;
}
[/code]

Share this post


Link to post
Share on other sites
deus.ex.nova    569
[quote name='SiCrane' timestamp='1355617410' post='5011107']
If you really wanted to do this you could use:
[code]
T SortedList<T>::Delete(void) {
T temp(m_pFront->GetData());
// do the deletion logic
return temp;
}
[/code]
[/quote]

Wait...so is the C-style initialization the only way to copy the reference value? In other words, this does work / is not allowed?
[CODE]
T noCopy = m_pFront->GetData();
[/CODE]

Share this post


Link to post
Share on other sites
SiCrane    11839
[tt]T temp(initializer);[/tt] and [tt]T temp = initializer;[/tt] will both create a copy of the the object. However, the first form is a direct initializer and the second form is a copy initializer. Under most circumstances you can treat them as the same, but there are times when the first will work but the second won't.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this