Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 Robot Ninja   Members   -  Reputation: 569

Like
0Likes
Like

Posted 15 December 2012 - 03:37 PM

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.

Edited by Robot Ninja, 15 December 2012 - 03:39 PM.


Sponsor:

#2 iMalc   Crossbones+   -  Reputation: 2299

Like
0Likes
Like

Posted 15 December 2012 - 04:00 PM

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

#3 Robot Ninja   Members   -  Reputation: 569

Like
0Likes
Like

Posted 15 December 2012 - 04:35 PM

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.

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.

Edited by Robot Ninja, 15 December 2012 - 04:41 PM.


#4 Paradigm Shifter   Crossbones+   -  Reputation: 5254

Like
0Likes
Like

Posted 15 December 2012 - 04:41 PM

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

#5 Robot Ninja   Members   -  Reputation: 569

Like
0Likes
Like

Posted 15 December 2012 - 04:48 PM

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...

Edited by Robot Ninja, 15 December 2012 - 04:48 PM.


#6 Paradigm Shifter   Crossbones+   -  Reputation: 5254

Like
1Likes
Like

Posted 15 December 2012 - 04:51 PM

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

#7 Robot Ninja   Members   -  Reputation: 569

Like
0Likes
Like

Posted 15 December 2012 - 04:55 PM

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? Posted Image

#8 Paradigm Shifter   Crossbones+   -  Reputation: 5254

Like
1Likes
Like

Posted 15 December 2012 - 04:58 PM

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

#9 Robot Ninja   Members   -  Reputation: 569

Like
0Likes
Like

Posted 15 December 2012 - 05:06 PM

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. Posted Image

#10 Paradigm Shifter   Crossbones+   -  Reputation: 5254

Like
0Likes
Like

Posted 15 December 2012 - 05:09 PM

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

#11 SiCrane   Moderators   -  Reputation: 9559

Like
2Likes
Like

Posted 15 December 2012 - 06:23 PM

If you really wanted to do this you could use:
T SortedList<T>::Delete(void) {
  T temp(m_pFront->GetData());
  // do the deletion logic
  return temp;
}


#12 Robot Ninja   Members   -  Reputation: 569

Like
0Likes
Like

Posted 15 December 2012 - 06:30 PM

If you really wanted to do this you could use:

T SortedList<T>::Delete(void) {
  T temp(m_pFront->GetData());
  // do the deletion logic
  return temp;
}


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


#13 Paradigm Shifter   Crossbones+   -  Reputation: 5254

Like
0Likes
Like

Posted 15 December 2012 - 06:38 PM

That should work if T has a copy constructor. It will make a copy though, so noCopy is not a good name for the variable ;)
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#14 SiCrane   Moderators   -  Reputation: 9559

Like
0Likes
Like

Posted 15 December 2012 - 06:51 PM

T temp(initializer); and T temp = initializer; 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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS