Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a function of which return type is an auto_ptr, i have no idea what i should return if user input an invalid parameter.
auto_ptr< myclass >& myFunction(int value)
{
	switch(value)
	{
	case 1:
		return myClassObject1;	// type of myClassObject1 is auto_ptr< myclass >
	case 2:
		return myClassObject2;	// type of myClassObject2 is auto_ptr< myclass >
	default:
		break;
	}
	// What should it return here?
}



I'd like to use it like this:
auto_pty< myclass >& myObj = myFunction(value);
if (myObj.get() == NULL)
{
	// Do something
}



So, my question is what the return value of myFunction should be when input value = 3.

BTW, MyClass is an abstract base class.

Thanks.
Posted
Updated 5-Jul-10 20:50pm
v2
Comments
szh121 6-Jul-10 3:07am    
I have changed my solution with another way, so this issue is not what i have to face to. Might there is impossible to resolve it in the case like this.

try
switch(...)
{
    ...
}
static auto_ptr default_val;
return default_val;


this will ensure the default value will survive the function return.

-TIP-
It tooks me a while to understand that auto_ptr was not std::auto_ptr... can you avoid misleading names ?
 
Share this answer
 
Comments
szh121 6-Jul-10 2:46am    
Sorry emilio_grv, it is std::auto_ptr actually. I forgot to add using namespace std; in my post.
szh121 6-Jul-10 2:51am    
I got you, i have to change add space to both side of myclass, otherwise it would get lost after posting.
Jörgen Sigvardsson 16-Jul-10 16:48pm    
static auto_ptr default_val;
return default_val;


That is a bad idea...
Emilio Garavaglia 17-Jul-10 5:01am    
I know ... but it respect the function signature.
The other very bad is
return *(auto_ptr*)0;
and he have to check for !&result || !result before doing result->something.
Return a default constructed auto_ptr:

return std::auto_ptr<interface>();</interface>


This should have a value of zero.

Cheers,

Ash

PS: If you're using a VC++ 2010 look at using unique_ptr instead. It's not got some of the surprising features of auto_ptr.
 
Share this answer
 
Comments
Emilio Garavaglia 2-Jul-10 12:35pm    
doesn't work. He has to return a referece to an auto_ptr, not to a "destroyed auto_ptr"...
Aescleal 2-Jul-10 13:06pm    
I see your point - he has to change the function signature as well.

Basically it'll teach him never to use auto_ptrs by reference or destruction will ensue.

Cheers,

Ash
szh121 6-Jul-10 3:01am    
Hi Aescleal, I am using VS 2005, and i get a compile error to use std::auto_ptr().
Maybe you are right, there is no such feather of auto_ptr can do sth like this.
Thanks for your reply.
Aescleal 6-Jul-10 12:25pm    
auto_ptr is designed to pass ownership around. For what you seem to be doing (looks like you're managing a set of class factories) I'd be tempted to maintain the factories by value and return references to them. One of the factories could be a null objects (has the same interface but no implementation) to handle the default case.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900