Java vs ++. , , , . :
Fruit GetFruit(std::string fruitName) {
if(fruitName != "apple" && fruitName != "banana")
{
fruitName = "kumquat";
}
return Fruit(fruitName);
}
However, this code will lead to the fact that the object itself (including all its internal data) will be copied in the return, as well as if it is copied in the future.
To be more Java-esque, you would use instead boost::shared_ptr. Then you deal with a reference counted object, as in Java:
boost::shared_ptr<Fruit> GetFruit(std::string fruitName) {
if(fruitName != "apple" && fruitName != "banana")
{
fruitName = "kumquat";
}
return new Fruit(fruitName);
}
source
share