• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
deus.ex.nova

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

13 posts in this topic

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
0

Share this post


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

Share this post


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

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

Share this post


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

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

Share this post


Link to post
Share on other sites
[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]
0

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

Share this post


Link to post
Share on other sites
[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]
0

Share this post


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

Share this post


Link to post
Share on other sites
[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]
0

Share this post


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

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  
Followers 0