I am fairly new to C++ and I know of three ways of returning a local variable and all have their downsides:
Person& getPerson()
{
Person bob;
return bob;
}
Clearly not a good idea.
Person getPerson()
{
Person bob;
return bob;
}
No chance of a null pointer or dangling reference but a performance hit.
Person* getPerson()
{
return new Person();
}
No chance of a null pointer but surely this violates the basic rules of OO design. Another object will have to delete this - but why should it have to? The implemenation of the getPerson() method has nothing to do with it.
So, I am looking for an alternative. I have heard of shared pointers and smart pointers (standard and Boost) but I'm not sure whether any of them are designed to deal with this problem. What do you guys suggest?