Skip to main content
gamedev.net
🔒 Locked

Programming problem

Started by The C modest god Oct 19, 2005 at 1:17 AM 3 replies 350+ views
Original Post
The C modest god
The C modest god
I have the following code:

template <class T>
inline T &
LinkedList<T>::Peek()
{
	if (this->pList==NULL)
	{
		T Dummy;
		GlobalErrorWrap::HandleDefaultError(DEFERR_LINKEDLIST_INVALIDEMPTYOPERATION);
		return Dummy;
	}
	return this->pList->Data;
}
I am throwing an exception in case of the specified error. However, on theory the HandleDefaultError does not necesseraly throws an exception and thus I cant rely on that the method wont continue. So I have to return from the method, but I have to return a value. I get a warnning on returning a local variable as a reference. Do you have a solution to this problem?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
RDragon1
RDragon1
Yes. Use std::list :)
Nypyren
Nypyren
[Edited]

Throw your own exception after HandleDefaultError.

If HandleDefaultError throws an exception, yours will be skipped. If not, yours will throw. Better than returning something that won't be used properly.
The C modest god
The C modest god
Quote:
Original post by Nypyren
Make the dummy a static member of the class.

Brilliant! Thanks.

It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
Nypyren
Nypyren
If your program is expecting to handle exceptions, then you might as well use exceptions rather than the dummy variable.

My original response was focused at the "returning reference to local variable" problem.

(Sorry, it's pretty late here)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.